#2456

Most Popular Video Creator

Medium
ArrayHash TableStringSortingHeap (Priority Queue)Hash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

This approach leverages hash tables to efficiently track the total views and the most viewed video for each creator in a single pass through the data. This reduces the time complexity significantly.

⚙️

Algorithm

3 steps
  1. 1Step 1: Create a hash map to store total views for each creator and another to store the most viewed video for each creator.
  2. 2Step 2: Iterate through the lists, updating the total views and checking if the current video has more views than the previously recorded most viewed video.
  3. 3Step 3: After processing all videos, determine the maximum popularity and gather all creators with that popularity along with their most viewed video IDs.
solution.py15 lines
1def mostPopularCreator(creators, ids, views):
2    creator_views = {}
3    creator_max_video = {}
4
5    for creator, video_id, view in zip(creators, ids, views):
6        if creator not in creator_views:
7            creator_views[creator] = 0
8            creator_max_video[creator] = (0, '')
9        creator_views[creator] += view
10        if view > creator_max_video[creator][0] or (view == creator_max_video[creator][0] and video_id < creator_max_video[creator][1]):
11            creator_max_video[creator] = (view, video_id)
12
13    max_popularity = max(creator_views.values())
14    result = [[creator, creator_max_video[creator][1]] for creator in creator_views if creator_views[creator] == max_popularity]
15    return result

Complexity note: The time complexity is O(n) because we only need to iterate through the lists once, and the space complexity is O(n) due to the storage of views and video IDs in hash maps.

  • 1Using hash maps allows for efficient aggregation of data.
  • 2Tracking multiple attributes (like views and video IDs) can be done simultaneously.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.