Most Popular Video Creator
Problem
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 strings answer 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.
Examples
Example 1
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 is 5 + 5 = 10.
The popularity of bob is 10.
The popularity of chris is 4.
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.
Example 2
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.
Constraints
n == creators.length == ids.length == views.length1 <= n <= 10^51 <= creators[i].length, ids[i].length <= 5creators[i]andids[i]consist only of lowercase English letters.0 <= views[i] <= 10^5
Solution
Method 1 - Hash Map + Sorting
Intuition
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.
Approach
- Iterate through all videos, for each creator:
- Sum up their total views.
- Track the most viewed video id (lex smallest if tie).
- Find the maximum popularity among all creators.
- For each creator with max popularity, add [creator, best_id] to the result.
Code
Java
import java.util.*;
class Solution {
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;
}
}
Python
def mostPopularCreator(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 not in 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]
Complexity
- ⏰ Time complexity:
O(n)— One pass to aggregate, one pass to collect results. - 🧺 Space complexity:
O(n)— For hash maps.