Loud and Rich LeetCode solution in Python

1
class Solution:
2
def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
3
length = len(quiet)
4
arr = [i for i in range(length)]
5
indegree = [0 for _ in range(length)]
6
graph = collections.defaultdict(list)
7
dq = collections.deque([])
8

9
for a, b in richer:
10
# Note that the graph is uni-directional
11
graph[a].append(b)
12
indegree[b] += 1
13

14
for i in range(length):
15
if not indegree[i]:
16
dq.append(i)
17

18
while dq:
19
node = dq.popleft()
20

21
for vertex in graph[node]:
22
indegree[vertex] -= 1
23
if quiet[arr[node]] < quiet[arr[vertex]]:
24
arr[vertex] = arr[node]
25
if not indegree[vertex]:
26
dq.append(vertex)
27
return arr

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0