-
Notifications
You must be signed in to change notification settings - Fork 7
/
course_schedule_ii.py
32 lines (28 loc) · 1.05 KB
/
course_schedule_ii.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
28
29
30
31
32
from collections import defaultdict
class Solution: # 56ms, Topological Sort, mixed with cycle detection
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
# build graph first
d = defaultdict(list)
for c,p in prerequisites:
d[p].append(c)
visited = [False for i in range(numCourses)]
recStack = [False for i in range(numCourses)]
stack = []
# topological sort
def ts(src):
visited[src] = True
recStack[src] = True
for child in d[src]:
if visited[child] == False:
if ts(child) == True:
return True
elif recStack[child] == True:
return True
stack.append(src)
recStack[src] = False
return False
for i in range(numCourses):
if visited[i] == False:
if ts(i) == True:
return []
return stack[::-1]