bumpsoo 2025-02-12 11:29:24 +00:00
parent 362b79ae66
commit d8f26e03e8

View 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