Similar Problems

Similar Problems not available

Finding The Users Active Minutes - Leetcode Solution

Companies:

LeetCode:  Finding The Users Active Minutes Leetcode Solution

Difficulty: Medium

Topics: hash-table array  

Problem Statement:

You are given the logs for the 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 1D integer array answer where answer[j] is the number of users whose UAM equals j.

Return the array answer sorted in non-decreasing order.

Example:

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. So the user has a UAM of 2 (minutes 2 and 5), which is mapped to answer[2]. The user with ID=1 performed actions at minutes 2 and 3. So they have a UAM of 2 (minutes 2 and 3), which is mapped to answer[2]. There is no user with a UAM of 1 or 3, so answer[0] = answer[3] = answer[4] = 0.

Solution

We will solve the problem in the following steps.

Step 1: Create a dictionary with unique users and the corresponding number of minutes of activity for that user.

Step 2: Using the dictionary created in step 1, create a list of length k+1.

Step 3: For each user in the dictionary, increment the count of the UAM in the list.

Step 4: Return the list.

Python Code:

def findingUsersActiveMinutes(logs, k):

#Step 1
activity = {}
for user,minute in logs:
    if user not in activity:
        activity[user] = set()
    activity[user].add(minute)

#Step 2
result = [0]*(k+1)

#Step 3
for user in activity:
    uam = len(activity[user])
    result[uam] += 1

#Step 4
return result

Time Complexity:

The time complexity of the algorithm is O(n), where n is the number of logs. This is because we iterate over all the logs once, and the other steps take constant time.

Space Complexity:

The space complexity of the algorithm is O(n), where n is the number of logs. This is because we need to store the activity of each user in a dictionary, which can have at most n entries.

Finding The Users Active Minutes Solution Code

1