-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetWatchedVideosbyYourFriends.py
36 lines (36 loc) · 1.15 KB
/
GetWatchedVideosbyYourFriends.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
class Solution:
def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]:
'''
bfs + heap
time: nlogn
space: n
'''
def bfs(start):
queue = [start]
current_level = 0
visited = set()
while queue:
if current_level >= level:
break
subqueue = queue[:]
queue = []
for i in subqueue:
visited.add(i)
for j in friends[i]:
if j not in visited and j not in queue and j not in subqueue:
queue.append(j)
current_level += 1
return queue
from collections import Counter
import heapq
videos = []
for i in bfs(id):
videos += watchedVideos[i]
freq = dict(Counter(videos))
heap = []
for key in freq:
heapq.heappush(heap, (freq[key], key))
ans = []
while heap:
ans.append(heapq.heappop(heap)[1])
return ans