array and hashmap heap December 25, 2022

Most popular video creator

Time O(nlog(n)) Space O(n) Open original problem

We will count the number of videos for each creator. Then we will iterate over the videos and count the number of views for each creator. We will update the result.

class Solution:
    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
        popular = collections.defaultdict(int)
        video = collections.defaultdict(list)

        for creator, _id, view in zip(creators, ids, views):
            popular[creator] += view
            video[creator].append((-view, _id))

        res, mostPopular = [], max(popular.values())
        for p in popular:
            if popular[p] == mostPopular:
                heapq.heapify(video[p])
                _, k = heapq.heappop(video[p])
                res.append([p, k])

        return res

Time complexity: O(nlog(n))
Space complexity: O(n)