forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loud and Rich.py
27 lines (23 loc) · 885 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
26
27
class Solution:
def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
length = len(quiet)
arr = [i for i in range(length)]
indegree = [0 for _ in range(length)]
graph = collections.defaultdict(list)
dq = collections.deque([])
for a, b in richer:
# Note that the graph is uni-directional
graph[a].append(b)
indegree[b] += 1
for i in range(length):
if not indegree[i]:
dq.append(i)
while dq:
node = dq.popleft()
for vertex in graph[node]:
indegree[vertex] -= 1
if quiet[arr[node]] < quiet[arr[vertex]]:
arr[vertex] = arr[node]
if not indegree[vertex]:
dq.append(vertex)
return arr