This commit is contained in:
parent
362b79ae66
commit
d8f26e03e8
1 changed files with 25 additions and 0 deletions
25
medium/max_sum_of_a_pair_with_eqaul_sum_of_digits.py
Normal file
25
medium/max_sum_of_a_pair_with_eqaul_sum_of_digits.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits
|
||||
|
||||
from typing import Dict, List
|
||||
import heapq
|
||||
|
||||
class Solution:
|
||||
def digit_sum(self, num: int) -> int:
|
||||
result = 0
|
||||
while num > 0:
|
||||
result += num % 10
|
||||
num //= 10
|
||||
return result
|
||||
def maximumSum(self, nums: List[int]) -> int:
|
||||
d: Dict[int, list[int]] = {}
|
||||
for num in nums:
|
||||
s = self.digit_sum(num)
|
||||
l = d.get(s, [])
|
||||
heapq.heappush(l, -num)
|
||||
d[s] = l
|
||||
result = -1
|
||||
for k in d:
|
||||
if len(d[k]) > 1:
|
||||
result = max(result, -heapq.heappop(d[k]) - heapq.heappop(d[k]))
|
||||
return result
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue