Problem Description
Given a list of logs where each log is a pair [ID, time], you need to compute each user's active minutes (unique minutes they performed an action) and then return a 1-indexed array answer of size k where answer[j] represents the number of users with exactly j active minutes.
Key Insights
- Use a hash table (or dictionary) to map each user ID to a set of the minutes in which they performed actions.
- Sets naturally handle duplicate minutes, ensuring that each minute is only counted once per user.
- After processing the logs, iterate through the dictionary to count the number of users who have a specific number of unique active minutes.
- Finally, construct the result array of length k (1-indexed) reflecting the count for each possible UAM from 1 to k.
Space and Time Complexity
Time Complexity: O(n), where n is the number of logs (each log is processed once, and set operations are near constant time on average).
Space Complexity: O(n), due to storage for the dictionary mapping user IDs to sets of minutes.
Solution
The solution uses a hash table (or dictionary) to group the logs by user ID. For every log [user, time], add the time to a set associated with that user. This automatically ensures that each minute is counted only once per user.
Once all logs are processed, iterate through the dictionary to compute the number of unique minutes for each user (the size of their set). Then, for each count, increment the corresponding index in the answer array (adjusting since the answer array is 1-indexed).
Finally, return the answer array.