forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmost-popular-video-creator.cpp
More file actions
31 lines (30 loc) · 957 Bytes
/
Copy pathmost-popular-video-creator.cpp
File metadata and controls
31 lines (30 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Time: O(n)
// Space: O(n)
// hash table
class Solution {
public:
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
unordered_map<string, int> cnt;
unordered_map<string, pair<int, string>> lookup;
for (int i = 0; i < size(creators); ++i) {
cnt[creators[i]] += views[i];
if (!lookup.count(creators[i])) {
lookup[creators[i]] = {-views[i], ids[i]};
continue;
}
lookup[creators[i]] = min(lookup[creators[i]], pair(-views[i], ids[i]));
}
int mx = 0;
for (const auto& [_, v] : cnt) {
mx = max(mx, v);
}
vector<vector<string>> result;
for (const auto& [k, v] : cnt) {
if (v != mx) {
continue;
}
result.push_back({k, lookup[k].second});
}
return result;
}
};