Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode solution in Python

1
class Solution:
2
# 668 ms, 99.52%. Time: O(NlogN). Space: O(N)
3
def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]:
4

5
def is_within_1hr(t1, t2):
6
h1, m1 = t1.split(":")
7
h2, m2 = t2.split(":")
8
if int(h1) + 1 < int(h2):
9
return False
10
if h1 == h2:
11
return True
12
return m1 >= m2
13

14
records = collections.defaultdict(list)
15
for name, time in zip(keyName, keyTime):
16
records[name].append(time)
17

18
rv = []
19
for person, record in records.items():
20
record.sort()
21
# Loop through 2 values at a time and check if they are within 1 hour.
22
if any(is_within_1hr(t1, t2) for t1, t2 in zip(record, record[2:])):
23
rv.append(person)
24
return sorted(rv)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0