Finding the Users Active Minutes LeetCode solution in Java

1
class Solution {
2
public int[] findingUsersActiveMinutes(int[][] logs, int k) {
3
// create a hashmap to record the ids of users
4
// and a set to store the minutes in which they were active
5
Map<Integer, Set<Integer>> map = new HashMap<>();
6
for (int[] l : logs) {
7
// l[0] -> id, l[1] -> active minute
8
map.putIfAbsent(l[0], new HashSet<>()); // if new id
9
map.get(l[0]).add(l[1]); // add the minute of activeness to the set
10
}
11

12
// create answer array
13
int[] ans = new int[k];
14
for (int id : map.keySet()) {
15
// the set contains all the minutes in which the id was active
16
// so the set size will be the number of active minutes
17
// or the 'User Active Minutes' (UAM)
18
int uam = map.get(id).size();
19
// each index in ans array is a UAM,
20
// and we need to put the number of users having that UAM
21
// whenever we enocunter a UAM, we increment the value in array
22
// which means we have found one more user with the same UAM
23
ans[uam - 1]++; // since array is 1 based indexing, we substract 1
24
}
25

26
return ans; // return ans array
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0