-
Notifications
You must be signed in to change notification settings - Fork 17
/
largest-time-for-given-digits.py
47 lines (33 loc) · 1.44 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
from typing import List, Tuple
class Solution:
def largestTimeFromDigits(self, A: List[int]) -> str:
taken = [False, False, False, False]
def get_hour(digits: List[int]) -> Tuple[str, str]:
for first_pos, first in enumerate(digits):
for second_pos, second in enumerate(digits):
if first_pos == second_pos:
continue
if (hour := first * 10 + second) < 24:
taken[first_pos] = True
taken[second_pos] = True
minute = get_minute(digits)
taken[first_pos] = False
taken[second_pos] = False
if minute:
return str(hour).zfill(2), minute
return "", ""
def get_minute(digits: List[int]) -> str:
for first_pos, first in enumerate(digits):
for second_pos, second in enumerate(digits):
if first_pos == second_pos:
continue
if taken[first_pos] or taken[second_pos]:
continue
if (minute := first * 10 + second) < 60:
return str(minute).zfill(2)
return ""
A.sort(reverse=True)
hour, minute = get_hour(A)
if hour and minute:
return hour + ":" + minute
return ""