Finding the Users Active Minutes - Problem

You are given the logs for users' actions on LeetCode, and an integer k. The logs are represented by a 2D integer array logs where each logs[i] = [IDi, timei] indicates that the user with IDi performed an action at the minute timei.

Multiple users can perform actions simultaneously, and a single user can perform multiple actions in the same minute.

The user active minutes (UAM) for a given user is defined as the number of unique minutes in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.

You are to calculate a 1-indexed array answer of size k such that, for each j (1 <= j <= k), answer[j] is the number of users whose UAM equals j.

Return the array answer as described above.

Input & Output

Example 1 — Basic Case
$ Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
Output: [0,2,0,0,0]
💡 Note: User 0 has UAM=2 (minutes 2,5), User 1 has UAM=2 (minutes 2,3). So 0 users have UAM=1, 2 users have UAM=2, and 0 users have UAM=3,4,5.
Example 2 — Single User
$ Input: logs = [[1,1],[1,2],[1,3]], k = 4
Output: [0,0,1,0]
💡 Note: User 1 has UAM=3 (minutes 1,2,3). So 1 user has UAM=3, others are 0.
Example 3 — Duplicate Minutes
$ Input: logs = [[0,1],[0,1],[1,1]], k = 3
Output: [2,0,0]
💡 Note: User 0 has UAM=1 (minute 1 counted once), User 1 has UAM=1 (minute 1). So 2 users have UAM=1.

Constraints

  • 1 ≤ logs.length ≤ 104
  • 0 ≤ IDi ≤ 109
  • 1 ≤ timei ≤ 105
  • k is in the range [1, logs.length]

Visualization

Tap to expand
Finding the Users Active Minutes INPUT logs = [[ID, time], ...] [0, 5] [1, 2] [0, 2] [0, 5] [1, 3] User Actions: User 0 times: 5, 2, 5 (dup 5) User 1 times: 2, 3 (unique) k = 5 Result array size ALGORITHM STEPS 1 Create HashMap Map user ID to Set of times 0 --> Set{5, 2} 1 --> Set{2, 3} 2 Process Logs Add time to user's Set 3 Calculate UAM UAM = Set size (unique mins) User 0: |{5,2}| = 2 UAM User 1: |{2,3}| = 2 UAM 4 Count Users per UAM answer[UAM]++ for each user answer[2]++ (User 0) answer[2]++ (User 1) FINAL RESULT Answer Array (1-indexed): j=1 j=2 j=3 j=4 j=5 0 2 0 0 0 answer[2] = 2 means: 2 users have UAM of 2 Breakdown: User 0: UAM=2 (mins 2,5) User 1: UAM=2 (mins 2,3) Output: [0, 2, 0, 0, 0] OK - Verified Key Insight: Using a HashMap with Set values automatically handles duplicate minutes for each user. The Set size gives us the UAM (unique active minutes) directly. Time: O(n), Space: O(n) where n is the number of log entries. Hash operations are O(1) average case. TutorialsPoint - Finding the Users Active Minutes | Hash Approach
Asked in
LeetCode 15 Amazon 8
12.0K Views
Medium Frequency
~15 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen