Leetcode 1604: Alert Using Same Key-Card Three or More Times in a One Hour Period

grid47
grid47
Exploring patterns and algorithms
May 30, 2024 6 min read

Employees use key-cards to unlock office doors, and the system records the time of each use. An alert is triggered if a worker uses their key-card three or more times within a one-hour period.
Problem
Approach
Steps
Complexity
Input: The input consists of two lists: keyName and keyTime. keyName[i] represents the name of the employee, and keyTime[i] represents the time the key-card was used by that employee.
Example: ["alice", "alice", "alice", "bob", "bob", "bob", "bob"]
Constraints:
• 1 <= keyName.length, keyTime.length <= 10^5
• keyName[i] contains only lowercase English letters
• keyTime[i] is in the format 'HH:MM'
Output: Return a list of unique employee names who used their key-card three or more times within a one-hour window. The result should be sorted in ascending alphabetical order.
Example: ["bob"]
Constraints:
• The names are returned sorted in ascending order
Goal: Sort the times for each employee, check for any sequence of three or more uses within a one-hour window, and return those names.
Steps:
• Iterate over each employee and convert their usage times to minutes.
• Sort the times of each employee.
• For each employee, check if there are three or more uses within a 60-minute window.
• If so, add the employee's name to the result list.
Goal: The constraints ensure that we are working with manageable data size, so sorting and comparing times should be efficient.
Steps:
• keyName and keyTime are both of the same length
• keyTime[i] is in the format 'HH:MM'
Assumptions:
• Key-card usage times are always within a single day.
Input: ["alice", "alice", "alice", "bob", "bob", "bob", "bob"]
Explanation: "bob" triggered an alert because their key-card was used 3 times within a one-hour period.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus