rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

1817. 查找用户活跃分钟数 #36

Open Ni-Guvara opened 1 year ago

Ni-Guvara commented 1 year ago

class Solution {
public:
    vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {

        vector<int> ret(k);   // 返回答案

        set<pair<int,int>> aux;       // 辅助map
        map<int,int> aux_map;
        int len = logs.size();  // 统计记录数

        for(int idx = 0; idx < len ; idx++)  // 去重
        {
            pair<int,int> p;
            p.first = logs[idx][0];
            p.second = logs[idx][1];

            aux.insert(p);
        }
        for(auto it = aux.begin();it != aux.end();it++)
        {
            ++aux_map[it->first];
        }

        for(auto it = aux_map.begin(); it != aux_map.end(); it++)
        {
            ++ret[it->second -1 ];
        }
        return ret;
    }
};
rocksc30 commented 1 year ago
class Solution {
    public int[] findingUsersActiveMinutes(int[][] logs, int k) {
        Map<Integer, HashSet<Integer>> map = new HashMap<>();
        for(int i = 0; i < logs.length; i++){
            // 记录 k,v => 用户, 分钟
            if(map.containsKey(logs[i][0])){
                HashSet<Integer> set = map.get(logs[i][0]);
                set.add(logs[i][1]);
                map.put(logs[i][0], set);
            }else{
                HashSet<Integer> cur = new HashSet<>();
                cur.add(logs[i][1]);
                map.put(logs[i][0], cur);
            }

        }
        int[] ret = new int[k];
        map.forEach((key, value) -> {
            ret[value.size() - 1]++;

        });
        return ret;
    }
}