-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path539.py
More file actions
65 lines (55 loc) · 1.97 KB
/
539.py
File metadata and controls
65 lines (55 loc) · 1.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
__________________________________________________________________________________________________
sample 64 ms submission
from itertools import cycle
class Solution:
def findMinDifference(self, timePoints: List[str]) -> int:
if len(timePoints) >= 1440:
return 0
s = set()
for time in timePoints:
if time in s:
return 0
s.add(time)
timePoints = sorted(s)
def time_diff(a, b):
h1, m1 = a.split(':')
h2, m2 = b.split(':')
secs = abs(60 * (int(h1) - int(h2)) + int(m1) - int(m2))
return min(secs, 1440 - secs)
next_iter = cycle(timePoints)
next(next_iter)
return min(
time_diff(nxt, prev)
for prev, nxt in zip(timePoints, next_iter)
)
__________________________________________________________________________________________________
sample 68 ms submission
from typing import List
class Solution:
MINS_IN_HOUR = 60
HOURS_IN_DAY = 24
MINS_IN_DAY = HOURS_IN_DAY * MINS_IN_HOUR
def findMinDifference(self, times: List[str]) -> int:
all_mins = set()
for time in times:
mins = self.get_mins(time)
if mins in all_mins:
return 0
all_mins.add(mins)
result = self.MINS_IN_DAY
prev, first = -1, -1
for t in range(self.MINS_IN_DAY):
if t not in all_mins:
continue
if prev == -1:
prev = first = t
else:
result = min(result, t-prev)
prev = t
over_midnight = self.MINS_IN_DAY - prev + first
result = min(result, over_midnight)
return result
def get_mins(self, time: str) -> int:
hours, mins = time.split(":")
return int(hours) * self.MINS_IN_HOUR + int(mins)
__________________________________________________________________________________________________