Problem Description
Given three arrays — creators, ids, and views — each video on a platform is represented by these arrays where creators[i] created a video with id ids[i] that got views[i] views. The popularity of a creator is defined as the sum of views on all of their videos. The task is to find the creator(s) with the highest popularity and, for each such creator, determine the id of their most popular video. In case of ties in the video view count, the lexicographically smallest id should be used.
Key Insights
- Use a hash map to accumulate the total number of views (popularity) for each creator.
- Use another hash map to track the best video (maximum views and lexicographically smallest id on tie) for each creator.
- First pass: iterate through the videos once to update total views and best video information per creator.
- Second pass: determine the maximum popularity among all creators and collect the results.
Space and Time Complexity
Time Complexity: O(n), where n is the number of videos, as we process each video once. Space Complexity: O(k), where k is the number of unique creators.
Solution
We solve the problem by iterating over the input arrays exactly once. As we iterate, we update two data structures:
- A dictionary (or hash map) that maps each creator to the total views from all their videos.
- A dictionary (or hash map) that maps each creator to a pair consisting of the highest view count seen and the corresponding video id. When a video’s view count ties with the current best, we update the stored id only if the new id is lexicographically smaller. Finally, we extract the maximum popularity value, and then for each creator with that popularity, add the creator and the id of their best video to the result.
Code Solutions
Below are implementations in Python, JavaScript, C++, and Java with line-by-line comments.