Alert Using Same Key-Card Three or More Times in a One Hour Period Problem
Description
LeetCode Problem 1604.
LeetCode company workers use key-cards to unlock office doors. Each time a worker uses their key-card, the security system saves the worker's name and the time when it was used. The system emits an alert if any worker uses the key-card three or more times in a one-hour period.
You are given a list of strings keyName and keyTime where [keyName[i], keyTime[i]] corresponds to a person's name and the time when their key-card was used in a single day.
Access times are given in the 24-hour time format "HH:MM", such as "23:51" and "09:49".
Return a list of unique worker names who received an alert for frequent keycard use. Sort the names in ascending order alphabetically.
Notice that "10:00" - "11:00" is considered to be within a one-hour period, while "22:51" - "23:52" is not considered to be within a one-hour period.
Example 1:
Input: keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"], keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"]
Output: ["daniel"]
Explanation: "daniel" used the keycard 3 times in a one-hour period ("10:00","10:40", "11:00").
Example 2:
Input: keyName = ["alice","alice","alice","bob","bob","bob","bob"], keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
Output: ["bob"]
Explanation: "bob" used the keycard 3 times in a one-hour period ("21:00","21:20", "21:30").
Constraints:
1 <= keyName.length, keyTime.length <= 10^5keyName.length == keyTime.lengthkeyTime[i]is in the format "HH:MM".[keyName[i], keyTime[i]]is unique.1 <= keyName[i].length <= 10keyName[i] contains only lowercase English letters.
Solution
Approach: Group by Name, Sort the Times
First group the uses by worker: a hash map from each name to the list of times, each converted from "HH:MM" to minutes after midnight (HH * 60 + MM), so times are plain numbers.
For each worker, sort the times. Three uses fall within one hour exactly when some three consecutive sorted times do: if t[i], t[j], t[k] (with i < j < k) are within 60 minutes, then so are t[k-2], t[k-1], t[k], which lie between t[i] and t[k]. So check whether t[i + 2] - t[i] <= 60 for some i.
The limit is inclusive: "10:00" and "11:00" are 60 minutes apart and count as one hour. Times are all within one day, so there is no wrapping past midnight: "23:59" and "00:01" are almost a day apart.
Finally sort the names of the alerted workers alphabetically.
Complexity: O(n log n) time for the sorting and O(n) extra space, where n is the number of key-card uses.
class Solution:
def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]:
minutes = {} # name -> times the card was used, in minutes after midnight
for name, t in zip(keyName, keyTime):
minutes.setdefault(name, []).append(int(t[:2]) * 60 + int(t[3:]))
ans = []
for name, ts in minutes.items():
ts.sort()
# Three uses within an hour <=> some three consecutive sorted uses span at most 60 minutes.
if any(ts[i + 2] - ts[i] <= 60 for i in range(len(ts) - 2)):
ans.append(name)
ans.sort()
return ans