Problem Description
Given a list of employee access records with employee names and times (in HHMM format), determine which employees accessed the system at least three times within any one-hour period (where one-hour is defined as any period strictly less than 60 minutes apart).
Key Insights
- Group the access times by employee name.
- Convert the access times from string (HHMM) to a numeric value (minutes since midnight) for easier arithmetic comparisons.
- Sort each employee's list of access times.
- For each sorted list, use a sliding window technique to check any contiguous segment where the difference between the earliest and the latest time in the window is less than 60 minutes and the count is at least three.
- Return the list of employees that meet the high-access condition.
Space and Time Complexity
Time Complexity: O(n log n) where n is the number of access records, due to the sorting step for each employee. Space Complexity: O(n) for storing access time data grouped by employee.
Solution
We first group the access times by employee using a hash table (or dictionary). For each employee, we convert the given HHMM string into minutes since midnight to simplify time difference computations. Next, we sort these time values. Then, using a sliding window approach, we iterate through the sorted list. For each window, we check if the difference between the current time and the time at the beginning of the window is strictly less than 60 minutes. If we find a window containing three or more entries, we mark that employee as high-access. Finally, we return the list of such employees.