Skip to content

Commit

Permalink
Create max-sum-of-a-pair-with-equal-sum-of-digits.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jul 19, 2022
1 parent a9e3576 commit 5c583ce
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Python/max-sum-of-a-pair-with-equal-sum-of-digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Time: O(nlogr), r is max(nums)
# Space: O(n)

# greedy
class Solution(object):
def maximumSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def sum_digits(x):
result = 0
while x:
result += x%10
x //= 10
return result

lookup = {}
result = -1
for x in nums:
k = sum_digits(x)
if k not in lookup:
lookup[k] = x
continue
result = max(result, lookup[k]+x)
if x > lookup[k]:
lookup[k] = x
return result

0 comments on commit 5c583ce

Please sign in to comment.