Leetcode 1817. Finding the Users Active Minutes Solution
Problem Explanation
You are given a 2D integer array logs
, where each log entry contains a user ID and the minute they performed an action on LeetCode. Your task is to calculate the user active minutes (UAM) for each user and return an array answer
of size k
, where answer[j]
is the number of users whose UAM equals j
. A user's UAM is defined as the number of unique minutes in which the user performed an action on LeetCode.
Approach
The solution involves using hashmaps and hashsets as data structures. We can use a hashmap to store the user ID as the key and a hashset of actions performed by the user as the values. Then, we can iterate through the hashmap to count the UAM for each user and increment the corresponding index in the answer array.
Let's walk through an example to understand the solution better.
Example 1:
logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
- Create a hashmap
idToTimes
to store the user ID as the key and a hashset of actions performed as the values. idToTimes = {} - Iterate through the logs and update the hashmap:
- logs[0] = [0, 5] -> idToTimes = {0: {5}}
- logs[1] = [1, 2] -> idToTimes = {0: {5}, 1: {2}}
- logs[2] = [0, 2] -> idToTimes = {0: {5, 2}, 1: {2}}
- logs[3] = [0, 5] -> idToTimes = {0: {5, 2}, 1: {2}}
- logs[4] = [1, 3] -> idToTimes = {0: {5, 2}, 1: {2, 3}}
- Create an array
ans
of lengthk
and initialize it with zeroes. ans = [0, 0, 0, 0, 0] - Iterate through the hashmap and increment the corresponding index in the answer array:
- For user 0, UAM = 2 -> ans = [0, 0, 1, 0, 0]
- For user 1, UAM = 2 -> ans = [0, 0, 2, 0, 0]
- Return the answer array: [0, 0, 2, 0, 0]
Solution
C++ Solution
1#include <vector>
2#include <unordered_map>
3#include <unordered_set>
4
5class Solution {
6 public:
7 std::vector<int> findingUsersActiveMinutes(std::vector<std::vector<int>>& logs, int k) {
8 std::vector<int> ans(k);
9 std::unordered_map<int, std::unordered_set<int>> idToTimes;
10
11 // Iterate through logs and store unique times for each user in hashmap
12 for (const std::vector<int>& log : logs) {
13 idToTimes[log[0]].insert(log[1]);
14 }
15
16 // Iterate through hashmap to calculate UAM for each user and update ans array
17 for (const auto& keyValue : idToTimes) {
18 int userUAM = keyValue.second.size();
19 ++ans[userUAM - 1];
20 }
21
22 return ans;
23 }
24};
Python Solution
1from collections import defaultdict
2from typing import List
3
4class Solution:
5 def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:
6 ans = [0] * k
7 idToTimes = defaultdict(set)
8
9 # Iterate through logs and store unique times for each user in hashmap
10 for user, time in logs:
11 idToTimes[user].add(time)
12
13 # Iterate through hashmap to calculate UAM for each user and update ans array
14 for times in idToTimes.values():
15 userUAM = len(times)
16 ans[userUAM - 1] += 1
17
18 return ans
Java Solution
1import java.util.HashMap;
2import java.util.HashSet;
3import java.util.Arrays;
4
5class Solution {
6 public int[] findingUsersActiveMinutes(int[][] logs, int k) {
7 int[] ans = new int[k];
8 HashMap<Integer, HashSet<Integer>> idToTimes = new HashMap<>();
9
10 // Iterate through logs and store unique times for each user in hashmap
11 for (int[] log : logs) {
12 int user = log[0];
13 int time = log[1];
14 idToTimes.putIfAbsent(user, new HashSet<>());
15 idToTimes.get(user).add(time);
16 }
17
18 // Iterate through hashmap to calculate UAM for each user and update ans array
19 for (HashSet<Integer> times : idToTimes.values()) {
20 int userUAM = times.size();
21 ans[userUAM - 1]++;
22 }
23
24 return ans;
25 }
26}
JavaScript Solution
1class Solution {
2 findingUsersActiveMinutes(logs, k) {
3 const ans = new Array(k).fill(0);
4 const idToTimes = new Map();
5
6 // Iterate through logs and store unique times for each user in hashmap
7 for (const [user, time] of logs) {
8 if (!idToTimes.has(user)) {
9 idToTimes.set(user, new Set());
10 }
11 idToTimes.get(user).add(time);
12 }
13
14 // Iterate through hashmap to calculate UAM for each user and update ans array
15 for (const times of idToTimes.values()) {
16 const userUAM = times.size;
17 ans[userUAM - 1]++;
18 }
19
20 return ans;
21 }
22}
C# Solution
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public int[] FindingUsersActiveMinutes(int[][] logs, int k) {
6 int[] ans = new int[k];
7 Dictionary<int, HashSet<int>> idToTimes = new Dictionary<int, HashSet<int>>();
8
9 // Iterate through logs and store unique times for each user in hashmap
10 foreach (int[] log in logs) {
11 int user = log[0];
12 int time = log[1];
13 if (!idToTimes.ContainsKey(user)) {
14 idToTimes[user] = new HashSet<int>();
15 }
16 idToTimes[user].Add(time);
17 }
18
19 // Iterate through hashmap to calculate UAM for each user and update ans array
20 foreach (HashSet<int> times in idToTimes.Values) {
21 int userUAM = times.Count;
22 ans[userUAM - 1]++;
23 }
24
25 return ans;
26 }
27}
Conclusion
In this article, we discussed the steps to find the Users Active Minutes for the given logs using a hashmap and a hashset as our data structures. The hashset helps in maintaining unique minutes for each user. We then loop through the hashmap to calculate the UAM for each user and update the answer array accordingly. Finally, we looked at the solution in different programming languages, i.e., C++, Python, Java, JavaScript, and C#.