-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1262GreatestSumDivisiblebyThree.py
37 lines (35 loc) · 1.11 KB
/
1262GreatestSumDivisiblebyThree.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
class Solution:
def maxSumDivThree(self, nums: List[int]) -> int:
left1 = []
left2 = []
ans = 0
for num in nums:
if num % 3 == 1:
left1.append(num)
if num % 3 == 2:
left2.append(num)
ans += num
if ans % 3 == 0:
return ans
left1.sort()
left2.sort()
if ans % 3 == 1:
if len(left1) > 0 and len(left2) > 1:
temp = min(left1[0] , left2[0] + left2[1])
return ans - temp
elif len(left1) > 0:
return ans - left1[0]
elif len(left2) > 1:
return ans - (left2[0] + left2[1])
else:
return 0
if ans % 3 == 2:
if len(left1) > 1 and len(left2) > 0:
temp = min(left1[0] + left1[1] , left2[0] )
return ans - temp
elif len(left2) > 0:
return ans - left2[0]
elif len(left1) > 1:
return ans - (left1[0] + left1[1])
else:
return 0