[LeetCode] Most Popular Video Creator

2456. Most Popular Video Creator

You are given two string arrays creators and ids, and an integer array views, all of length n. The ith video on a platform was created by creator[i], has an id of ids[i], and has views[i] views.

The popularity of a creator is the sum of the number of views on all of the creator’s videos. Find the creator with the highest popularity and the id of their most viewed video.

  • If multiple creators have the highest popularity, find all of them.
  • If multiple videos have the highest view count for a creator, find the lexicographically smallest id.

Return a 2D array of strings answer where answer[i] = [creatori, idi] means that creatori has the highest popularity and idi is the id of their most popular video. The answer can be returned in any order.

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
32
33
class Solution {
public:
vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
unordered_map<string, long long> freq;
unordered_map<string, string> name;
unordered_map<string, long long> view;
long long ma = -1;
for(int i = 0; i < creators.size(); i++) {
string c = creators[i];
string id = ids[i];
long long v = views[i];
if(!name.count(c)) name[c] = id;
if(view[c] == v) {
name[c] = min(name[c], id);

} else if(view[c] < v) {
name[c] = id;
}
view[c] = max(view[c], v);
freq[c] += v;
ma = max(ma, freq[c]);
}
vector<vector<string>> res;
for(auto [k,v] : freq) {
if(v == ma) {
res.push_back({k,name[k]});
}
}
return res;
}
};


Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/30/PS/LeetCode/most-popular-video-creator/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.