Watch 10 video solutions for Most Popular Video Creator, a medium level problem involving Array, Hash Table, String. This walkthrough by Bro Coders has 1,417 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
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.
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 <= 1051 <= creators[i].length, ids[i].length <= 5creators[i] and ids[i] consist only of lowercase English letters.0 <= views[i] <= 105Problem Overview: You receive three arrays: creators, ids, and views. Each index represents a video. The task is to identify the creator(s) with the highest total views across all their videos, and return the id of their most viewed video (lexicographically smallest id if there is a tie).
Approach 1: HashMap Aggregation (O(n) time, O(n) space)
The cleanest approach aggregates statistics per creator using a hash map. Iterate through the arrays once and store two pieces of information for each creator: the cumulative view count and the most popular video for that creator. While processing each video, update the total views and compare the current video's views with the stored best video. If views are higher, or equal but lexicographically smaller, update the best id. After the pass finishes, compute the maximum total views among creators and collect all creators matching that value. Hash lookups keep updates constant time, making the full solution linear. This approach relies heavily on hash table operations and sequential scanning of the array inputs.
Approach 2: Two-Pass Process with Sorting (O(n log n) time, O(n) space)
Another option groups videos by creator and sorts each creator's videos by view count (descending) and id (ascending). First pass: build a map from creator to a list of their videos. Second pass: sort each creator's list so the most popular video becomes the first element. While iterating through creators, compute total views and track the maximum. Sorting simplifies selecting the best video but increases complexity to O(n log n). This approach is straightforward when you are already comfortable with sorting custom objects.
Approach 3: Map with Priority Queue (O(n log k) time, O(n) space)
Maintain a map where each creator tracks total views and a small priority queue of their videos. The heap orders videos by highest views and then by smallest id. As you process videos, push entries into the heap and update the cumulative views. The heap ensures the best video can always be retrieved in O(log k), where k is the number of videos for that creator. This pattern is useful when streaming input or when only the top element per creator matters. It uses a heap (priority queue) for efficient ranking.
Recommended for interviews: The HashMap aggregation approach is the expected solution. It solves the problem in a single pass with O(n) time and minimal bookkeeping. The sorting or heap approaches demonstrate alternative thinking but introduce unnecessary overhead for this specific problem.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| HashMap Aggregation | O(n) | O(n) | Best general solution. One-pass aggregation with constant-time hash lookups. |
| Two-Pass with Sorting | O(n log n) | O(n) | When grouping then sorting creator videos feels simpler than maintaining comparisons during iteration. |
| Map with Priority Queue | O(n log k) | O(n) | Useful when processing streaming video data or frequently retrieving the top video per creator. |