-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax Pair Sum in an Array.py
48 lines (29 loc) · 1.27 KB
/
Max Pair Sum in an Array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal.
Return the maximum sum or -1 if no such pair exists.
'''
class Solution:
def getmaxdigit(self, n):
curmaxd = list(str(n))
curmaxd = max([int(x) for x in curmaxd])
return curmaxd
def maxSum(self, nums: List[int]) -> int:
res = -1
for i in range(len(nums)):
curmaxd = self.getmaxdigit(nums[i])
for j in range(i+1, len(nums)):
curm2 = self.getmaxdigit(nums[j])
if curm2 == curmaxd:
res = max(res, nums[i]+nums[j])
if res == -1:
return -1
return res
---------------------------------------------------------------------------------------------
class Solution:
def maxSum(self, nums: List[int]) -> int:
d, ans = defaultdict(list), -1
for num in nums: d[max(str(num))].append(num) # <-- 1)
for ch in d:
if len(d[ch]) < 2: continue
ans = max(ans, sum(sorted(d[ch])[-2:])) # <-- 2)
return ans # <-- 3)