Skip to content

Commit 7bada76

Browse files
Merge pull request #2190 from CodingTestStudy2/변지협
[변지협] 2026/10/17
2 parents 44c6e45 + 88575ef commit 7bada76

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
1. 아이디어 :
3+
id에서 출발해 BFS를 돌면서 거리(level)를 함께 큐에 저장한다.
4+
거리가 level인 친구들을 모은 뒤, 그 친구들이 본 영상들을 세어서
5+
(시청 횟수, 제목) 오름차순으로 정렬해 반환한다.
6+
2. 시간복잡도 :
7+
o(V + E + N log N) — 그래프 탐색 V+E, 영상 정렬 N log N
8+
3. 자료구조/알고리즘 :
9+
BFS, defaultdict, Counter
10+
11+
'''
12+
13+
from collections import defaultdict, deque, Counter
14+
15+
class Solution:
16+
def watchedVideosByFriends(self, watchedVideos: list[list[str]], friends: list[list[int]], id: int, level: int) -> list[str]:
17+
n = len(friends)
18+
friend_map = defaultdict(list)
19+
for i, friend in enumerate(friends):
20+
friend_map[i].extend(friend)
21+
22+
visited = [False] * n
23+
queue = deque()
24+
queue.append((0, id))
25+
visited[id] = True
26+
27+
levels = []
28+
while queue:
29+
i, current = queue.pop()
30+
31+
if i == level:
32+
levels.append(current)
33+
else:
34+
for nxt in friend_map[current]:
35+
if not visited[nxt]:
36+
visited[nxt] = True
37+
queue.append((i + 1, nxt))
38+
39+
ans = []
40+
for i in levels:
41+
for j in watchedVideos[i]:
42+
ans.append(j)
43+
44+
return [key for key, value in sorted(Counter(ans).items(), key=lambda x: (x[1], x[0]))]

0 commit comments

Comments
 (0)