Alert Using Same Key-Card Three or More Times in a One Hour Period - Problem

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.

Note: "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.

Input & Output

Example 1 — Mixed Violations
$ 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 used keycard at 10:00, 10:40, 11:00 - the span is exactly 60 minutes (11:00 - 10:00), which triggers an alert. luis has times spread across 6 hours, no 3 accesses within 60 minutes.
Example 2 — No Violations
$ 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"]
💡 Note: alice's times 12:00, 12:01, 18:00 span 6 hours (360 minutes > 60). bob's times 21:00, 21:20, 21:30 span only 30 minutes ≤ 60, triggering an alert.
Example 3 — Multiple Violations
$ Input: keyName = ["john","john","john","john","mary","mary","mary"], keyTime = ["23:58","23:59","00:01","00:02","20:00","20:30","20:59"]
Output: ["john","mary"]
💡 Note: john: times 23:58, 23:59, 00:01 span from 1438 to 1441 minutes (3 minutes apart). mary: 20:00, 20:30, 20:59 span 59 minutes. Both violate the rule.

Constraints

  • 1 ≤ keyName.length, keyTime.length ≤ 105
  • keyName.length == keyTime.length
  • keyTime[i] is in the format "HH:MM"
  • [keyName[i], keyTime[i]] is unique
  • 1 ≤ keyName[i].length ≤ 10
  • keyName[i] contains only lowercase English letters

Visualization

Tap to expand
Key-Card Alert Detection INPUT keyName[]: daniel daniel daniel luis luis luis luis keyTime[]: 10:00 10:40 11:00 09:00 11:00 13:00 15:00 Visual Timeline: Daniel: 10:00 10:40 11:00 Luis: 09:00 11:00 13:00 15:00 1-hour window ALGORITHM STEPS 1 Group by Name Map: name --> [times] daniel: [10:00, 10:40, 11:00] luis: [09:00, 11:00,13:00,15:00] 2 Sort Times Sort each person's times 3 Sliding Window Check 3 consecutive times Daniel check: time[2] - time[0] 11:00 - 10:00 = 60 min <= 60 4 Check Condition If diff <= 60 min: ALERT Luis check: Gaps: 2h, 2h, 2h > 60 OK FINAL RESULT DANIEL - ALERT! 3 accesses in 60 minutes: 10:00, 10:40, 11:00 (11:00 - 10:00 = 60 min) LUIS - OK No 3 accesses within 1 hour 09:00, 11:00, 13:00, 15:00 Output: ["daniel"] Sorted alphabetically Key Insight: After sorting times for each person, we only need to check if time[i+2] - time[i] <= 60 minutes. If 3 consecutive sorted times fit in 1 hour, there's an alert. No need to check all combinations! Time complexity: O(n log n) for sorting. Space: O(n) for the hashmap grouping. TutorialsPoint - Alert Using Same Key-Card Three or More Times in a One Hour Period | Sort + Sliding Window
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen