#1817

Finding the Users Active Minutes

Medium
ArrayHash TableHash MapArray
LeetCode ↗

Approaches

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

Intuition

Time O(n)Space O(n)

The optimal solution uses a hash map to efficiently track unique minutes for each user, allowing us to compute the user active minutes in linear time.

⚙️

Algorithm

4 steps
  1. 1Step 1: Initialize a hash map to store unique minutes for each user.
  2. 2Step 2: Iterate through the logs and populate the hash map with unique minutes for each user.
  3. 3Step 3: Create an answer array of size k initialized to zero.
  4. 4Step 4: Count the number of users for each unique active minute and populate the answer array.
solution.py13 lines
1# Full working Python code
2from collections import defaultdict
3
4def findingUsersActiveMinutes(logs, k):
5    user_minutes = defaultdict(set)
6    for user_id, time in logs:
7        user_minutes[user_id].add(time)
8    
9    answer = [0] * k
10    for minutes in user_minutes.values():
11        uam = len(minutes)
12        answer[uam - 1] += 1
13    return answer

Complexity note: The time complexity is O(n) because we process each log entry once. The space complexity is O(n) due to the storage of unique minutes for each user.

  • 1Using a hash map allows for efficient tracking of unique minutes.
  • 2Counting users based on their unique active minutes can be done in a single pass after building the map.

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