Advertisement

High-Access Employees - LeetCode 2933 Solution

High-Access Employees - Complete Solution Guide

High-Access Employees is LeetCode problem 2933, 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

You are given a 2D 0-indexed array of strings, access_times , with size n . For each i where 0 <= i <= n - 1 , access_times[i][0] represents the name of an employee, and access_times[i][1] represents the access time of that employee. All entries in access_times are within the same day. The access time is represented as four digits using a 24-hour time format, for example, "0800" or "2250" . An employee is said to be high-access if he has accessed the system three or more times within a one-hour

Detailed Explanation

The problem requires identifying 'high-access' employees from a list of employee access times. An employee is considered high-access if they have accessed the system three or more times within a one-hour period. The access times are given in a 24-hour format string (e.g., '0800', '2250'). The input is a 2D array of strings, where each row represents an employee's name and access time. The output should be a list of the names of high-access employees, with no duplicates and in any order. Crucially, times must fall within the same one-hour window to be counted.

Solution Approach

The solution approach involves the following steps: First, group the access times by employee using a hash map (dictionary). Then, for each employee, sort their access times numerically. Finally, iterate through the sorted access times and check if there are at least three access times within a one-hour (100 minutes in the time representation) window. If an employee meets this condition, add their name to the list of high-access employees.

Step-by-Step Algorithm

  1. Step 1: Create a hash map (dictionary) to store access times grouped by employee name.
  2. Step 2: Iterate through the input `access_times` array and populate the hash map. Convert the access time strings to integers and store them in a list associated with the employee's name.
  3. Step 3: Create an empty list to store the names of high-access employees.
  4. Step 4: Iterate through the hash map, processing each employee and their access times.
  5. Step 5: Sort the access times for each employee in ascending order.
  6. Step 6: Check if the employee has at least three access times. If not, skip to the next employee.
  7. Step 7: Iterate through the sorted access times using a sliding window of size 3. For each window, check if the difference between the earliest and latest access time is less than 100 (one hour).
  8. Step 8: If the difference is less than 100, it means there are three access times within one hour. Add the employee's name to the list of high-access employees and break out of the inner loop (as the employee is confirmed to be high-access).
  9. Step 9: Return the list of high-access employees.

Key Insights

  • Insight 1: The problem requires grouping access times by employee and then checking for overlapping intervals (within a one-hour range).
  • Insight 2: Sorting the access times for each employee is crucial to efficiently check for the one-hour period constraint.
  • Insight 3: Converting the time strings to integers simplifies comparison and calculations.

Complexity Analysis

Time Complexity: O(nlogn)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, String, Sorting.

Companies

Asked at: Atlassian.