forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloud-and-rich.py
26 lines (22 loc) · 830 Bytes
/
loud-and-rich.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
# Time: O(q + r)
# Space: O(q + r)
class Solution(object):
def loudAndRich(self, richer, quiet):
"""
:type richer: List[List[int]]
:type quiet: List[int]
:rtype: List[int]
"""
def dfs(graph, quiet, node, result):
if result[node] is None:
result[node] = node
for nei in graph[node]:
smallest_person = dfs(graph, quiet, nei, result)
if quiet[smallest_person] < quiet[result[node]]:
result[node] = smallest_person
return result[node]
graph = [[] for _ in xrange(len(quiet))]
for u, v in richer:
graph[v].append(u)
result = [None]*len(quiet)
return map(lambda x: dfs(graph, quiet, x, result), xrange(len(quiet)))