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 vector<int>& log : logs)
      idToTimes[log[0]].insert(log[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[] log : logs) {
      idToTimes.putIfAbsent(log[0], new HashSet<>());
      idToTimes.get(log[0]).add(log[1]);
    }

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

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

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

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