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
creators[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.
Note: It is possible for different videos to have the same id, meaning that
ids do not uniquely identify a video. For example, two videos with the same ID are considered as distinct videos with their own viewcount.
Return __ a 2D array of stringsanswer where answer[i] = [creatorsi, idi] means that creatorsi has the highest popularity and
idi is the id of their most popular video. The answer can be returned in any order.
Input: creators =["alice","bob","alice","chris"], ids =["one","two","three","four"], views =[5,10,5,4]Output: [["alice","one"],["bob","two"]]Explanation:
The popularity of alice is5+5=10.The popularity of bob is10.The popularity of chris is4.alice and bob are the most popular creators.For bob, the video with the highest view count is"two".For alice, the videos with the highest view count are "one" and "three". Since
"one"is lexicographically smaller than "three", it is included in the answer.
Input: creators =["alice","alice","alice"], ids =["a","b","c"], views =[1,2,2]Output: [["alice","b"]]Explanation:
The videos with id "b" and "c" have the highest view count.Since "b"is lexicographically smaller than "c", it is included in the answer.
We need to find the creator(s) with the highest total views, and for each, the id of their most viewed video (choosing the lexicographically smallest id in case of ties). We use hash maps to aggregate views and track the best video for each creator.
import java.util.*;
classSolution {
public List<List<String>>mostPopularCreator(String[] creators, String[] ids, int[] views) {
Map<String, Long> pop =new HashMap<>();
Map<String, String> bestId =new HashMap<>();
Map<String, Integer> bestView =new HashMap<>();
int n = creators.length;
for (int i = 0; i < n; i++) {
String c = creators[i], id = ids[i];
int v = views[i];
pop.put(c, pop.getOrDefault(c, 0L) + v);
if (!bestId.containsKey(c) || v > bestView.get(c) || (v == bestView.get(c) && id.compareTo(bestId.get(c)) < 0)) {
bestId.put(c, id);
bestView.put(c, v);
}
}
long maxPop = Collections.max(pop.values());
List<List<String>> res =new ArrayList<>();
for (String c : pop.keySet()) {
if (pop.get(c) == maxPop) {
res.add(Arrays.asList(c, bestId.get(c)));
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
defmostPopularCreator(creators, ids, views):
from collections import defaultdict
pop = defaultdict(int)
best = {}
for c, id, v in zip(creators, ids, views):
pop[c] += v
if c notin best or v > best[c][0] or (v == best[c][0] and id < best[c][1]):
best[c] = (v, id)
max_pop = max(pop.values())
return [[c, best[c][1]] for c in pop if pop[c] == max_pop]