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.
This approach uses a hash map to aggregate data for each creator. We maintain a map to store the total views for each creator. Additionally, a secondary map keeps track of each creator's most-viewed video ID with tie-breaking based on lexicographical order. After processing, we extract creators with the maximum popularity and their top videos.
The solution uses a dictionary total_views to map creators to their aggregated views and another dictionary max_video to track each creator's most viewed video ID with views. As we iterate through the input list, we update these dictionaries. Finally, we determine the maximum popularity from total_views and extract creators matching this popularity, along with their top video IDs.
Python
Time Complexity: O(n), where n is the number of elements in the list, as we process each element once.
Space Complexity: O(m), where m is the number of unique creators since we store aggregated data for each creator.
This approach first calculates the total views for each creator in the first pass using a hash map. In the second pass, we determine the most popular creators (those with maximum aggregated views) and use sorting to resolve tie-breaking by selecting the lexicographically smallest video for each creator.
The first step is to calculate the total views for each creator using a dictionary. Next, we identify the maximum popularity and prepare a list of (views, ID) tuples for each candidate creator. We then sort these tuples first by negative views (to sort in descending order of views) and then lexicographically by ID. The top video for each creator is added to the final result list.
Python
Time Complexity: O(n log n) due to the sorting operation per creator. The initial aggregation is O(n).
Space Complexity: O(n), which accounts for storing all relevant video ID tuples for creators with the maximum popularity.
This approach involves using hash maps (or dictionaries in Python) to keep track of each creator's total views and their most viewed video. Iterate through the list of creators, and for each creator, accumulate their total views. Keep track of each video's view count and update the most viewed video for each creator, ensuring to select the lexicographically smallest ID in case of ties.
The mostPopularCreator function operates by iterating through each video's metadata, updating a dictionary popularity to track cumulative views for each creator, and another dictionary most_viewed that stores tuples of views and ids. After processing, it determines creators with the highest popularity and retrieves their most popular video ID.
Python
JavaScript
Time Complexity: O(n log n), due to sorting the list of videos for each creator. Space Complexity: O(n) for storing the views and ids of each creator.
This approach uses maps to track the total views per creator and priority queues to efficiently retrieve the highest viewed video for each creator. Here, a max-heap is used, and we maintain the heap property based on views and the lexicographical order of IDs in case of ties.
The Java solution uses a HashMap for cumulative view counting and a PriorityQueue for efficiently retrieving the creator's top video. The queue is customized based on view count primarily and lexically as a tie-breaker. This ensures that only the necessary data sorting is applied for maximal efficiency.
Time Complexity: O(n log n), due to priority queue operations. Space Complexity: O(n), for auxiliary data structures.
We traverse the three arrays, use a hash table cnt to count the total play count for each creator, and use a hash table d to record the index of the video with the highest play count for each creator.
Then, we traverse the hash table cnt to find the maximum play count mx; then we traverse the hash table cnt again to find the creators with a play count of mx, and add them to the answer array.
The time complexity is O(n), and the space complexity is O(n). Here, n is the number of videos.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Using HashMap for Aggregation | Time Complexity: O(n), where n is the number of elements in the list, as we process each element once. |
| Two-Pass Process with Sorting | Time Complexity: O(n log n) due to the sorting operation per creator. The initial aggregation is O(n). |
| Approach 1: Using HashMap/Dictionaries | Time Complexity: O(n log n), due to sorting the list of videos for each creator. Space Complexity: O(n) for storing the views and ids of each creator. |
| Approach 2: Using Map with PriorityQueue | Time Complexity: O(n log n), due to priority queue operations. Space Complexity: O(n), for auxiliary data structures. |
| Hash Table | — |
| 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. |
2456. Most Popular Video Creator | Weekly Contest 317 | LeetCode 2456 • Bro Coders • 1,417 views views
Watch 9 more video solutions →Practice Most Popular Video Creator with our built-in code editor and test cases.
Practice on FleetCode