You are given the logs for users' actions on LeetCode, and an integer k
. The logs are represented by a 2D integer array logs
where each logs[i] = [IDi, timei]
indicates that the user with IDi
performed an action at the minute timei
.
Multiple users can perform actions simultaneously, and a single user can perform multiple actions in the same minute.
The user active minutes (UAM) for a given user is defined as the number of unique minutes in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.
You are to calculate a 1-indexed array answer
of size k
such that, for each j
(1 <= j <= k
), answer[j]
is the number of users whose UAM equals j
.
Return the array answer
as described above.
Example 1:
Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5 Output: [0,2,0,0,0] Explanation: The user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once). The user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2. Since both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.
Example 2:
Input: logs = [[1,1],[2,2],[2,3]], k = 4 Output: [1,1,0,0] Explanation: The user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1. The user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2. There is one user with a UAM of 1 and one with a UAM of 2. Hence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.
Constraints:
1 <= logs.length <= 104
0 <= IDi <= 109
1 <= timei <= 105
k
is in the range[The maximum UAM for a user, 105]
.
查找用户活跃分钟数。
给你用户在 LeetCode 的操作日志,和一个整数 k 。日志用一个二维整数数组 logs 表示,其中每个 logs[i] = [IDi, timei] 表示 ID 为 IDi 的用户在 timei 分钟时执行了某个操作。
多个用户 可以同时执行操作,单个用户可以在同一分钟内执行 多个操作 。
指定用户的 用户活跃分钟数(user active minutes,UAM) 定义为用户对 LeetCode 执行操作的 唯一分钟数 。 即使一分钟内执行多个操作,也只能按一分钟计数。
请你统计用户活跃分钟数的分布情况,统计结果是一个长度为 k 且 下标从 1 开始计数 的数组 answer ,对于每个 j(1 <= j <= k),answer[j] 表示 用户活跃分钟数 等于 j 的用户数。
返回上面描述的答案数组 answer 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/finding-the-users-active-minutes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是用一个 HashMap<Integer, Set<Integer>> 记录每个用户都在哪些分钟执行了操作。注意活跃分钟数的定义是只要用户在某个分钟有了操作,无论操作多少次,都只按一分钟计数。所以这样记录下来之后,理论上每个用户对应的 hashset 里存的是一堆 unique 的分钟数。
最后要求返回的是一个长度为 K 的数组,表示拥有X个活跃分钟数的用户有几个。注意最后数组 index 的表达,题目说下标从 1 开始计数,是需要我们把活跃分钟数 - 1当做数组的下标。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public int[] findingUsersActiveMinutes(int[][] logs, int k) { 3 HashMap<Integer, Set<Integer>> map = new HashMap<>(); 4 for (int[] log : logs) { 5 if (!map.containsKey(log[0])) { 6 map.put(log[0], new HashSet<>()); 7 } 8 map.get(log[0]).add(log[1]); 9 } 10 11 int[] res = new int[k]; 12 for (int key : map.keySet()) { 13 int size = map.get(key).size(); 14 res[size - 1]++; 15 } 16 return res; 17 } 18 }
标签:1817,logs,int,answer,UAM,user,Active,LeetCode,Users From: https://www.cnblogs.com/cnoodle/p/17062363.html