Alert Using Same Key-Card Three or More Times in a One Hour Period - Complete Solution Guide
Alert Using Same Key-Card Three or More Times in a One Hour Period is LeetCode problem 1604, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 forma
Detailed Explanation
The problem asks us to identify employees who have used their keycards three or more times within any one-hour period on a given day. We are provided with two lists: `keyName` containing employee names, and `keyTime` containing the corresponding keycard usage times in "HH:MM" format. The goal is to return a sorted list of unique employee names who triggered an alert due to frequent keycard use.
Solution Approach
The solution involves first converting the 'HH:MM' time strings into integer representation in minutes from the start of the day. A hash map (dictionary) is then used to group access times for each employee. These times are sorted in ascending order for each employee. We then iterate through each employee's sorted access times, checking if there are at least three access times, and if there exist three accesses within a one-hour (60 minute) window. If this condition is met, the employee's name is added to a list of alerted users. Finally, the list of alerted users is sorted alphabetically and returned.
Step-by-Step Algorithm
- Step 1: Create a hash map (dictionary) to store each employee's name as the key and a list of their access times (converted to minutes) as the value.
- Step 2: Iterate through the `keyName` and `keyTime` lists. For each entry, convert the time string to minutes and append it to the corresponding employee's list in the hash map.
- Step 3: Iterate through the hash map. For each employee:
- Step 4: Sort the list of access times (in minutes) in ascending order.
- Step 5: If the number of access times is less than 3, skip to the next employee.
- Step 6: Iterate through the sorted access times, checking if there is a window of three accesses with a difference of 60 minutes or less. Specifically, check if `times[i + 2] - times[i] <= 60` for any valid index `i`.
- Step 7: If such a window is found, add the employee's name to the `alerted_users` list if it's not already present, and break the inner loop.
- Step 8: Sort the `alerted_users` list alphabetically.
- Step 9: Return the `alerted_users` list.
Key Insights
- Insight 1: Convert time strings to a numerical representation (minutes) for easier comparison.
- Insight 2: Group keycard usage times by employee name using a hash table.
- Insight 3: Sort the usage times for each employee to efficiently check for three accesses within an hour.
- Insight 4: Handle duplicates by adding alerted employee names only once to the result list.
Complexity Analysis
Time Complexity: O(nlogn)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, String, Sorting.
Companies
Asked at: Karat, Wayfair.