-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLargest Time for Given Digits.py
54 lines (46 loc) · 1.47 KB
/
Largest Time for Given Digits.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
49
50
51
52
53
54
class Solution:
def largestTimeFromDigits(self, arr: List[int]) -> str:
res = ""
digit_freq = collections.Counter(arr)
# first digit
if 2 in arr and sum([digit_freq[d] for d in range(6)]) > 2:
res += "2"
arr.remove(2)
else:
for digit in [1,0]:
if digit in arr:
res += str(digit)
arr.remove(digit)
break
# can't make 24-hour time
if len(res) == 0:
return ""
# second digit 0-3
if res == "2":
for digit in [3,2,1,0]:
if digit in arr:
res += str(digit)
arr.remove(digit)
break
# no 0-3 left in arr
if len(res) == 1:
return ""
# second digit 0-9
else:
for digit in range(9,-1,-1):
if digit in arr:
res += str(digit)
arr.remove(digit)
break
res += ":"
for digit in range(5, -1, -1):
if digit in arr:
res += str(digit)
arr.remove(digit)
break
if len(res) == 3:
return ""
for digit in range(9,-1,-1):
if digit in arr:
res += str(digit)
return res