首页 > 其他分享 >[LeetCode] 1817. Finding the Users Active Minutes

[LeetCode] 1817. Finding the Users Active Minutes

时间:2023-01-20 02:11:07浏览次数:56  
标签:1817 logs int answer UAM user Active LeetCode Users

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 }

 

LeetCode 题目总结

标签:1817,logs,int,answer,UAM,user,Active,LeetCode,Users
From: https://www.cnblogs.com/cnoodle/p/17062363.html

相关文章

  • IP 网络主动监测系统 Renix Active
    一、IT网络运维面临的挑战​1.网络性能可视化​•与公有云和SaaS平台连接的可靠性​•广域网线路性能​•互联网专线性能​2.诊断工具​•现场无IT工程师覆盖​•......
  • IP 网络主动监测系统 Renix Active
    ​一、IT网络运维面临的挑战​1.网络性能可视化​•与公有云和SaaS平台连接的可靠性​•广域网线路性能​•互联网专线性能​2.诊断工具​•现场无IT工程师覆盖......
  • tensorboard 可视化 No dashboards are active for the current data set.
    错误运行后出现:补救修正logs文件位置,添加绝对路径......
  • Azure Active Directory审核和监控
    AzureAD在许多组织中发挥着重要作用。但是,随着最近出现的许多安全攻击和行业监管机构发挥着前所未有的力量,即使是在AzureAD环境中最小的违规行为也会使您的组织陷入困境。......
  • 使用ActiveMQ Artemis进行重连
    正常创建一个连接,在一段时间后心跳就会因为接收不到数据而强制停止。从而断开连接,那么无论是前端还是后端都应该有自己的实现重连机制。这里写一个关于前端实现重连的机制:......
  • activeBg用到链接
    搭建视频:https://www.jianshu.com/p/d2795a6a06fbhttps://imgur.com/https://catbox.moe/https://pixabay.com/videos/search/livewallpaper/https://livewallpapers4......
  • active code page: 65001 提示去除
    Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\CommandProcessorPressWin+R.Type"regedit"andpress Enter.Navigatetothepath HKEY_LOCAL_MACHINE\SOFT......
  • FILIP: FINE-GRAINED INTERACTIVE LANGUAGEIMAGE PRE-TRAINING论文阅读笔记
    摘要目前的图像文本预训练模型通常通过每个模态全局特征的相似性来建模跨模态的交互,然而这会导致缺乏足够的信息;或者通过在视觉/文本token上使用跨模态注意力/自注意力来......
  • pycharm:无法加载文件 C:\Users\admin\PycharmProjects\pythonProject1\venv\Scr
    以前一直在vmware虚机上用pycharm,这次想在win10pc上试试 安装pycharm后,打开终端直接报错:无法加载文件C:\Users\admin\PycharmProjects\pythonProject1\venv\Scripts......
  • ActiveMQ和spring集成
    1.消息发送类packagecn.com.biceng.jms.queue;importjavax.jms.Destination;importjavax.jms.JMSException;importjavax.jms.Message;importjavax.jms.Session;import......