Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode solution in Python
1class Solution:2# 668 ms, 99.52%. Time: O(NlogN). Space: O(N)3def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]:45def is_within_1hr(t1, t2):6h1, m1 = t1.split(":")7h2, m2 = t2.split(":")8if int(h1) + 1 < int(h2):9return False10if h1 == h2:11return True12return m1 >= m21314records = collections.defaultdict(list)15for name, time in zip(keyName, keyTime):16records[name].append(time)1718rv = []19for person, record in records.items():20record.sort()21# Loop through 2 values at a time and check if they are within 1 hour.22if any(is_within_1hr(t1, t2) for t1, t2 in zip(record, record[2:])):23rv.append(person)24return sorted(rv)