#2933

High-Access Employees

Medium
ArrayHash TableStringSortingHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n log n)
Space
O(1)
O(n)
💡

Intuition

Time O(n log n)Space O(n)

This approach leverages sorting and a sliding window technique to efficiently count access times within one-hour periods. By sorting the access times first, we can easily check for counts without nested loops.

⚙️

Algorithm

3 steps
  1. 1Step 1: Create a dictionary to store access times for each employee.
  2. 2Step 2: For each employee, sort their access times.
  3. 3Step 3: Use a sliding window to count how many access times fall within a one-hour period.
solution.py17 lines
1from collections import defaultdict
2
3def high_access_employees(access_times):
4    access_dict = defaultdict(list)
5    for name, time in access_times:
6        access_dict[name].append(time)
7    high_access = []
8    for name, times in access_dict.items():
9        times.sort()
10        left = 0
11        for right in range(len(times)):
12            while int(times[right]) - int(times[left]) >= 60:
13                left += 1
14            if right - left >= 2:
15                high_access.append(name)
16                break
17    return list(set(high_access))

Complexity note: The time complexity is O(n log n) due to sorting the access times for each employee. The sliding window technique runs in O(n), making this approach efficient overall.

  • 1Sorting access times allows for efficient counting using a sliding window.
  • 2Using a dictionary to group access times by employee simplifies the problem.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.