Loud and Rich LeetCode solution in Python
1class Solution:2def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:3length = len(quiet)4arr = [i for i in range(length)]5indegree = [0 for _ in range(length)]6graph = collections.defaultdict(list)7dq = collections.deque([])89for a, b in richer:10# Note that the graph is uni-directional11graph[a].append(b)12indegree[b] += 11314for i in range(length):15if not indegree[i]:16dq.append(i)1718while dq:19node = dq.popleft()2021for vertex in graph[node]:22indegree[vertex] -= 123if quiet[arr[node]] < quiet[arr[vertex]]:24arr[vertex] = arr[node]25if not indegree[vertex]:26dq.append(vertex)27return arr