classSolution: deffindingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: mp = defaultdict(set) for i, t in logs: mp[i].add(t) ans = [0] * k for s in mp.values(): ans[len(s) - 1] += 1 return ans
publicclassSolution { publicint[] FindingUsersActiveMinutes(int[][] logs, int k) { IDictionary<int, ISet<int>> activeMinutes = new Dictionary<int, ISet<int>>(); foreach (int[] log in logs) { int id = log[0], time = log[1]; activeMinutes.TryAdd(id, new HashSet<int>()); activeMinutes[id].Add(time); } int[] answer = newint[k]; foreach (ISet<int> minutes in activeMinutes.Values) { int activeCount = minutes.Count; answer[activeCount - 1]++; } return answer; } }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var findingUsersActiveMinutes = function(logs, k) { const activeMinutes = newMap(); for (const [id, time] of logs) { if (!activeMinutes.has(id)) { activeMinutes.set(id, newSet()); } activeMinutes.get(id).add(time); } const answer = newArray(k).fill(0); for (const minutes of activeMinutes.values()) { const activeCount = minutes.size; answer[activeCount - 1]++; } return answer; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcfindingUsersActiveMinutes(logs [][]int, k int) []int { mp := map[int]map[int]bool{} for _, p := range logs { id, t := p[0], p[1] if mp[id] == nil { mp[id] = map[int]bool{} } mp[id][t] = true } ans := make([]int, k+1) for _, m := range mp { ans[len(m)]++ } return ans[1:] }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k){ unordered_map<int, unordered_set<int>> activeMinutes; for (auto &&log : logs) { int id = log[0], time = log[1]; activeMinutes[id].emplace(time); } vector<int> answer(k); for (auto &&[_, minutes] : activeMinutes) { int activeCount = minutes.size(); answer[activeCount - 1]++; } return answer; } };