Most Popular Video Creator — LeetCode 2456 Python Solution

MediumArrayHash TableStringSortingHeap (Priority Queue)
Problem
#2456
Reading time
2 min

The problem

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.

Python solution

Python
class Solution:
    def mostPopularCreator(
        self, creators: List[str], ids: List[str], views: List[int]
    ) -> List[List[str]]:
        cnt = defaultdict(int)
        d = defaultdict(int)
        for k, (c, i, v) in enumerate(zip(creators, ids, views)):
            cnt[c] += v
            if c not in d or views[d[c]] < v or (views[d[c]] == v and ids[d[c]] > i):
                d[c] = k
        mx = max(cnt.values())
        return [[c, ids[d[c]]] for c, x in cnt.items() if x == mx]

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Heap / Priority Queue

Keep only the best k elements, or always pull the smallest, in log time. LeetCode 2456. Most Popular Video Creator is filed here because LeetCode tags it Heap (Priority Queue), which is the vocabulary this hub collects.

The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2456. Most Popular Video Creator?
LeetCode 2456. Most Popular Video Creator is rated Medium on LeetCode.
What is the time complexity of LeetCode 2456. Most Popular Video Creator?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2456. Most Popular Video Creator?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2456. Most Popular Video Creator cover?
LeetCode 2456. Most Popular Video Creator is tagged Array, Hash Table, String, Sorting and Heap (Priority Queue) on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview