1817.查找用户活跃分钟数
链接:1817.查找用户活跃分钟数
难度:Medium
标签:数组、哈希表
简介:请你统计用户活跃分钟数的分布情况,统计结果是一个长度为 k 且 下标从 1 开始计数 的数组 answer ,对于每个 j(1 <= j <= k),answer[j] 表示 用户活跃分钟数 等于 j 的用户数。
题解 1 - rust
- 编辑时间:2023-01-20
- 执行用时:60ms
- 内存消耗:4.9MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
    pub fn finding_users_active_minutes(logs: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
        use std::collections::{HashMap, HashSet};
        let mut ans = vec![0; k as usize];
        let mut m = HashMap::<i32, HashSet<i32>>::new();
        for log in logs {
            let s = m.entry(log[0]).or_insert(HashSet::new());
            s.insert(log[1]);
        }
        for (_, v) in m {
            ans[v.len() - 1] += 1;
        }
        ans
    }
}
题解 2 - python
- 编辑时间:2023-01-20
- 执行用时:132ms
- 内存消耗:20.5MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:
    ans = [0 for _ in range(k)]
    m = {}
    for log in logs:
        s = m.setdefault(log[0], set())
        s.add(log[1])
    for (_, v) in m.items():
        ans[len(v) - 1] += 1
    return ans
题解 3 - cpp
- 编辑时间:2023-01-20
- 执行用时:208ms
- 内存消耗:83.4MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
public:
    vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {
        vector<int> list(k, 0);
        unordered_map<int, unordered_set<int>> m;
        for (auto &log : logs) m[log[0]].insert(log[1]);
        for (auto &user : m) list[user.second.size() - 1]++;
        return list;
    }
};