Problem statement

https://binarysearch.com/problems/K-Longest-Show-Durations/

Solution

Use counter here and then use most common.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, shows, durations, k):
        cnt = Counter()
        for d, s in zip(durations, shows):
            cnt[s] += d

        return sum(y for x, y in cnt.most_common(k))