{x}
blog image

Finding the Users Active Minutes

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.

 

Example 1:

Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
Output: [0,2,0,0,0]
Explanation:
The user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once).
The user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.
Since both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.

Example 2:

Input: logs = [[1,1],[2,2],[2,3]], k = 4
Output: [1,1,0,0]
Explanation:
The user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1.
The user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.
There is one user with a UAM of 1 and one with a UAM of 2.
Hence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.

 

Constraints:

  • 1 <= logs.length <= 104
  • 0 <= IDi <= 109
  • 1 <= timei <= 105
  • k is in the range [The maximum UAM for a user, 105].

Solution Explanation: Finding Users Active Minutes

This problem involves determining the number of unique minutes each user was active on a platform and then counting how many users had each specific number of active minutes. The solution uses a hash table (or dictionary in Python) to efficiently track user activity.

Approach:

  1. Data Structure: A hash table (map, dictionary) is used to store user IDs as keys. The values associated with each key are sets (or similar structures in other languages) containing the unique minutes the user was active. Sets are ideal because they automatically handle duplicate entries, ensuring we only count each minute once per user.

  2. Processing Logs: The code iterates through the input logs array. For each log entry [ID, time], it adds the time to the set associated with the ID in the hash table. If the user ID doesn't exist yet, a new set is created for that user.

  3. Calculating User Active Minutes (UAM): After processing all logs, the code iterates through the hash table. For each user, the size of their associated set (which represents the number of unique active minutes) is determined.

  4. Counting UAM Distribution: A result array answer of size k is initialized to all zeros. This array will store the count of users with each UAM value (from 1 to k). For each user's UAM, the corresponding index in answer is incremented.

  5. Returning the Result: Finally, the answer array, which now contains the distribution of UAMs, is returned.

Time Complexity Analysis:

  • Processing Logs: Iterating through the logs array takes O(n) time, where n is the length of the logs array. Inserting into a set takes O(1) on average, so the overall time complexity of this step is O(n).

  • Calculating UAM: Iterating through the hash table takes O(m) time, where m is the number of unique users. Getting the size of each set is O(1). Therefore this step is O(m). In the worst case, m could be equal to n (if each log entry represents a different user), but it will usually be less than n.

  • Counting UAM Distribution: This step takes O(m) time, as we iterate through the UAM values, which is at most the number of users (m).

Therefore, the overall time complexity is dominated by the iteration of logs and the subsequent iterations, resulting in O(n) where n is the number of logs. The space complexity is O(n) because the hash table could store up to n unique entries in the worst case (each log is a different user with a single action).

Code Examples (with slight variations for clarity):

The provided code examples are already quite efficient and well-structured. Here's a slightly modified Python version for better readability, with comments:

from collections import defaultdict
 
def findingUsersActiveMinutes(logs: list[list[int]], k: int) -> list[int]:
    """
    Calculates the distribution of user active minutes.
 
    Args:
        logs: A list of lists, where each inner list represents a user action [user_id, time].
        k: The maximum number of active minutes to consider.
 
    Returns:
        A list of integers representing the count of users with each number of active minutes (1 to k).
    """
 
    user_activity = defaultdict(set)  # Use defaultdict for easier handling of new users
 
    # Record user activity
    for user_id, time in logs:
        user_activity[user_id].add(time)
 
    uam_counts = [0] * k  # Initialize the count of users for each UAM value
 
    # Count the UAMs
    for user_id, active_minutes in user_activity.items():
        uam = len(active_minutes)
        if uam <= k:  # Only count if UAM is within the range
            uam_counts[uam - 1] += 1
 
    return uam_counts

The other language examples provided (Java, C++, Go, TypeScript) are equally efficient and follow the same core algorithmic approach. They differ only in syntax and the specific data structures used (e.g., HashMap vs. unordered_map, Set vs. unordered_set).