LeetCode 1817: Finding the Users Active Minutes

ArrayHash Table

Problem Description

Explanation

To solve this problem, we need to count the unique minutes in which each user performed an action and then count how many users have the same number of unique minutes. We can achieve this by using a HashMap to store the user IDs along with a HashSet of unique minutes for each user. Then, we can count the occurrences of unique minute counts and form the answer array.

  • Create a HashMap to store user IDs as keys and corresponding HashSet of unique minutes as values.
  • Iterate through the logs and populate the HashMap.
  • Count the number of unique minutes for each user.
  • Count the occurrences of unique minute counts and update the answer array accordingly.

Time complexity: O(n), where n is the number of logs
Space complexity: O(n)

Solutions

class Solution {
    public int[] findingUsersActiveMinutes(int[][] logs, int k) {
        Map<Integer, Set<Integer>> map = new HashMap<>();
        int[] answer = new int[k];
        
        for (int[] log : logs) {
            map.putIfAbsent(log[0], new HashSet<>());
            map.get(log[0]).add(log[1]);
        }
        
        for (int userId : map.keySet()) {
            int uam = map.get(userId).size();
            answer[uam - 1]++;
        }
        
        return answer;
    }
}

Loading editor...