Most Popular Video Creator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from collections import defaultdict

class Solution:
    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
        popularity = defaultdict(int)
        most_viewed_video = defaultdict(lambda: [float('-inf'), ''])

        # Calculating popularity and most viewed video for each creator
        for creator, video_id, view in zip(creators, ids, views):
            popularity[creator] += view

            # Update the most viewed video if the current view is greater
            # or if it has the same view but lexicographically smaller id
            if view > most_viewed_video[creator][0] or (view == most_viewed_video[creator][0] and video_id < most_viewed_video[creator][1]):
                most_viewed_video[creator] = [view, video_id]

        # Find the max popularity
        max_popularity = max(popularity.values())

        # Collect the creators with max popularity and their most viewed videos
        result = [[creator, most_viewed_video[creator][1]] for creator, pop in popularity.items() if pop == max_popularity]

        return result

Explanation:

  • We use dictionaries to keep track of the popularity and most viewed video for each creator.
  • We iterate through the given data and update the popularity and most viewed video accordingly.
  • Finally, we find the creators with the maximum popularity and collect their most viewed video id(s) in the result list.

This code will return a 2D array with the required information.