Skip to content

1817. Finding the Users Active Minutes

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {
    vector<int> ans(k);
    unordered_map<int, unordered_set<int>> idToTimes;

    for (const auto& l : logs)
      idToTimes[l[0]].insert(l[1]);

    for (const auto& [_, mins] : idToTimes)
      ++ans[mins.size() - 1];

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
  public int[] findingUsersActiveMinutes(int[][] logs, int k) {
    int[] ans = new int[k];
    Map<Integer, Set<Integer>> idToTimes = new HashMap<>();

    for (int[] l : logs) {
      idToTimes.putIfAbsent(l[0], new HashSet<>());
      idToTimes.get(l[0]).add(l[1]);
    }

    for (final int id : idToTimes.keySet())
      ++ans[idToTimes.get(id).size() - 1];

    return ans;
  }
}
1
2
3
4
5
6
7
8
9
class Solution:
  def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:
    idToTimes = defaultdict(set)

    for id, time in logs:
      idToTimes[id].add(time)

    c = Counter(len(times) for times in idToTimes.values())
    return [c[i] for i in range(1, k + 1)]
Back to top