1 parent 046a909 commit 9b60a01Copy full SHA for 9b60a01
1 file changed
leetcode3/최민지/851.py
@@ -0,0 +1,30 @@
1
+class Solution:
2
+ def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
3
+ n = len(quiet)
4
+ adj = [[] for _ in range(n)]
5
+
6
+ # u -> v: u가 v보다 돈이 많음
7
+ for u, v in richer:
8
+ adj[v].append(u)
9
10
+ res = [-1] * n
11
12
+ def dfs(node):
13
+ if res[node] != -1:
14
+ return res[node]
15
16
+ # 자기 자신이 일단 기본값
17
+ min_quiet_person = node
18
19
+ for parent in adj[node]:
20
+ candidate = dfs(parent)
21
+ if quiet[candidate] < quiet[min_quiet_person]:
22
+ min_quiet_person = candidate
23
24
+ res[node] = min_quiet_person
25
26
27
+ for i in range(n):
28
+ dfs(i)
29
30
+ return res
0 commit comments