-
Notifications
You must be signed in to change notification settings - Fork 481
/
1363.py
34 lines (30 loc) · 996 Bytes
/
1363.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
class Solution:
def largestMultipleOfThree(self, digits: List[int]) -> str:
cnt = [0] * 10
for i in digits: cnt[i] += 1
b1, b2 = cnt[1] + cnt[4] + cnt[7], cnt[2] + cnt[5] + cnt[8]
t = (b1 + b2 * 2) % 3
if t == 1:
if b1 >= 1: b1 -= 1
else: b2 -= 2
elif t == 2:
if b2 >= 1: b2 -= 1
else: b1 -= 2
res = ""
for i in range(9, -1, -1):
if i % 3 == 0:
while cnt[i]:
res += str(i)
cnt[i] -= 1
elif i % 3 == 1 and b1:
while cnt[i] and b1:
res += str(i)
cnt[i] -= 1
b1 -= 1
else:
while cnt[i] and b2:
res += str(i)
cnt[i] -= 1
b2 -= 1
if res and res[0] == "0": return "0"
return res