-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path210. Course Schedule II.py
More file actions
28 lines (24 loc) · 1.04 KB
/
210. Course Schedule II.py
File metadata and controls
28 lines (24 loc) · 1.04 KB
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
28
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
adj_list = collections.defaultdict(list)
in_degree = collections.defaultdict(int)
for u, v in prerequisites:
adj_list[v].append(u) # [[1,0]], so that adj_list[0] = [1, ...]
# in_degree
in_degree[u] += 1
# zero indegree queue
queue = collections.deque()
for k in range(numCourses):
if k not in in_degree:
queue.append(k)
res = [] ### topological_sorted_order
while queue:
vertex = queue.popleft()
res.append(vertex)
# Reduce in-degree for all the neighbors
if vertex in adj_list:
for neighbor in adj_list[vertex]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)
return res if len(res) == numCourses else []