Skip to content

Commit 5c583ce

Browse files
authored
Create max-sum-of-a-pair-with-equal-sum-of-digits.py
1 parent a9e3576 commit 5c583ce

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(nlogr), r is max(nums)
2+
# Space: O(n)
3+
4+
# greedy
5+
class Solution(object):
6+
def maximumSum(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
def sum_digits(x):
12+
result = 0
13+
while x:
14+
result += x%10
15+
x //= 10
16+
return result
17+
18+
lookup = {}
19+
result = -1
20+
for x in nums:
21+
k = sum_digits(x)
22+
if k not in lookup:
23+
lookup[k] = x
24+
continue
25+
result = max(result, lookup[k]+x)
26+
if x > lookup[k]:
27+
lookup[k] = x
28+
return result

0 commit comments

Comments
 (0)