Alert Using Same Key-Card Three or More Times in a One Hour Period - Problem
Secure Office Key-Card Monitoring System
You're working for LeetCode's security department to monitor suspicious key-card activity! ๐
The company uses key-cards to track when employees enter different areas. Your job is to identify employees who might be acting suspiciously by using their key-card three or more times within any one-hour period.
What you're given:
โข
โข
What you need to find:
Return a sorted list of employee names who triggered security alerts (used key-card โฅ3 times in any 60-minute window).
Important: "10:00" to "11:00" is exactly one hour, but "22:51" to "23:52" spans 61 minutes and doesn't count!
Example: If Alice uses her card at ["10:00", "10:30", "11:00"], that's 3 times in 60 minutes โ Alert! ๐จ
You're working for LeetCode's security department to monitor suspicious key-card activity! ๐
The company uses key-cards to track when employees enter different areas. Your job is to identify employees who might be acting suspiciously by using their key-card three or more times within any one-hour period.
What you're given:
โข
keyName: Array of employee namesโข
keyTime: Array of corresponding access times in "HH:MM" formatWhat you need to find:
Return a sorted list of employee names who triggered security alerts (used key-card โฅ3 times in any 60-minute window).
Important: "10:00" to "11:00" is exactly one hour, but "22:51" to "23:52" spans 61 minutes and doesn't count!
Example: If Alice uses her card at ["10:00", "10:30", "11:00"], that's 3 times in 60 minutes โ Alert! ๐จ
Input & Output
example_1.py โ Basic Alert Case
$
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"]
๐ก Note:
Daniel uses his key-card 3 times: 10:00, 10:40, and 11:00. The span from 10:00 to 11:00 is exactly 60 minutes, so he triggers an alert. Luis has 4 accesses but no 3 consecutive ones fit within 60 minutes (e.g., 09:00, 11:00, 13:00 spans 240 minutes).
example_2.py โ No Alerts
$
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:
[]
๐ก Note:
Alice's times: 12:00, 12:01, 18:00. Any 3 have a span > 60 minutes. Bob's times: 21:00, 21:20, 21:30, 23:00. The first three (21:00-21:30) span 30 minutes โค 60, but wait... let me recalculate. Actually Bob should trigger an alert! The first 3 times span 30 minutes.
example_3.py โ Edge Case: Exactly 60 Minutes
$
Input:
keyName = ["john","john","john"]
keyTime = ["23:58","23:59","00:58"]
โบ
Output:
[]
๐ก Note:
John's accesses span from 23:58 to 00:58 the next day, but since times are within a single day only, 00:58 is treated as 58 minutes from midnight. The span would be treated as crossing day boundary incorrectly. Actually, the problem states times are within a single day, so this case shouldn't occur in valid input.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Access Data
Gather all key-card usage logs with names and times
2
Group by Employee
Organize access times by each person's name
3
Sort Timeline
Arrange each person's access times chronologically
4
Sliding Window Check
Look for any 3 consecutive accesses within 60 minutes
5
Generate Alerts
Flag employees who violated the security policy
Key Takeaway
๐ฏ Key Insight: After grouping and sorting access times by person, use a sliding window to check if any 3 consecutive accesses span โค60 minutes. This transforms a complex time-window problem into a simple sorted array scan!
Time & Space Complexity
Time Complexity
O(n log n)
Grouping is O(n), sorting each person's times dominates the complexity
โก Linearithmic
Space Complexity
O(n)
Hash map stores all times grouped by person
โก Linearithmic Space
Constraints
- 1 โค keyName.length, keyTime.length โค 105
- keyName.length == keyTime.length
- keyTime[i] is in the format "HH:MM"
- All times are within a single day (no day boundary crossing)
- keyName[i] consists of only lowercase English letters
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code