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: []
💡 Note: alice's first 3 times: 12:00, 12:01, 18:00 span 6 hours. bob's tightest 3 times: 21:00, 21:20, 21:30 span only 30 minutes but that's within limit. No violations.
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 System OverviewInput: Access Recordsdaniel 10:00daniel 10:40daniel 11:00luis 09:00Step 1: Group by Employeedaniel10:00, 10:40, 11:0060 min span ≤ 60 ✓luis09:00, 11:00, 13:00240 min span > 60Step 2: Check Time WindowsSliding Window Check:For 3+ accesses, find if any3 consecutive times ≤ 60 min apartStep 3: Output ViolatorsAlert Triggered!Result: ["daniel"]Employees sorted alphabetically
Understanding the Visualization
1
Input
Employee names and access times throughout the day
2
Group & Sort
Organize times by employee and sort chronologically
3
Detect Violations
Find employees with 3+ accesses in any 60-minute window
Key Takeaway
🎯 Key Insight: Sort each employee's times, then sliding window efficiently detects 3+ accesses within 60 minutes
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