Largest Time for Given Digits LeetCode solution in Python

1
class Solution:
2
def largestTimeFromDigits(self, arr: List[int]) -> str:
3
res = ""
4
digit_freq = collections.Counter(arr)
5
# first digit
6

7
if 2 in arr and sum([digit_freq[d] for d in range(6)]) > 2:
8
res += "2"
9
arr.remove(2)
10
else:
11
for digit in [1, 0]:
12
if digit in arr:
13
res += str(digit)
14
arr.remove(digit)
15
break
16
# can't make 24-hour time
17
if len(res) == 0:
18
return ""
19

20
# second digit 0-3
21
if res == "2":
22
for digit in [3, 2, 1, 0]:
23
if digit in arr:
24
res += str(digit)
25
arr.remove(digit)
26
break
27
# no 0-3 left in arr
28
if len(res) == 1:
29
return ""
30
# second digit 0-9
31
else:
32
for digit in range(9, -1, -1):
33
if digit in arr:
34
res += str(digit)
35
arr.remove(digit)
36
break
37

38
res += ":"
39

40
for digit in range(5, -1, -1):
41
if digit in arr:
42
res += str(digit)
43
arr.remove(digit)
44
break
45

46
if len(res) == 3:
47
return ""
48

49
for digit in range(9, -1, -1):
50
if digit in arr:
51
res += str(digit)
52
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0