Skip to content

Commit acbfd35

Browse files
Merge pull request #1979 from CodingTestStudy2/최원준
[최원준] Day05
2 parents 2cd27ca + f45b132 commit acbfd35

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
3+
n = len(quiet)
4+
5+
graph = [[] for _ in range(n)]
6+
7+
for rich, poor in richer:
8+
graph[poor].append(rich)
9+
10+
answer = [-1] * n
11+
12+
def dfs(x):
13+
# 이미 계산했다면 재사용
14+
if answer[x] != -1:
15+
return answer[x]
16+
17+
# 자기 자신도 후보
18+
answer[x] = x
19+
20+
for rich in graph[x]:
21+
candidate = dfs(rich)
22+
23+
if quiet[candidate] < quiet[answer[x]]:
24+
answer[x] = candidate
25+
26+
return answer[x]
27+
28+
for i in range(n):
29+
dfs(i)
30+
31+
return answer

0 commit comments

Comments
 (0)