-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[Amazon] Most Popular Video Creator.py
54 lines (40 loc) · 1.57 KB
/
[Amazon] Most Popular Video Creator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Question: https://leetcode.com/problems/most-popular-video-creator/
# Medium
from typing import Optional, List
from collections import defaultdict
from heapq import heapify, heappop, heappush
class Solution:
# O(n) time and O(n) space
def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
def get_best_content_id(creator):
content = creator_info[creator]
heapify(content)
return [content[0][1]]
creator_info = defaultdict(list)
creator_popul = defaultdict(int)
for idx in range(len(creators)):
creator_info[creators[idx]].append((-views[idx], ids[idx]))
creator_popul[creators[idx]] += views[idx]
max_popul = None
best_creators = []
for creator, popularity in creator_popul.items():
if max_popul is None:
max_popul = popularity
best_creators.append(creator)
continue
if popularity == max_popul:
best_creators.append(creator)
elif popularity > max_popul:
max_popul = popularity
best_creators = [creator]
result = []
for creator in best_creators:
stats = [creator]
for content_id in get_best_content_id(creator):
stats.append(content_id)
result.append(stats)
return result
# January 30, 2023
'''
# Kunal Wadhwa
'''