-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
39 lines (32 loc) · 1.18 KB
/
solution.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
33
34
35
36
37
38
39
from typing import List
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
prereq = {c: [] for c in range(numCourses)}
for course, pre in prerequisites:
prereq[course].append(pre)
# first condition: visited -> course has been added to output
# second condition: visiting -> course not added to output, but added to cycle
# first condition: visited -> course not added to output or cycle
output = []
visit, cycle = set(), set()
def dfs(course):
if course in cycle:
return False
if course in visit:
return True
cycle.add(course)
for pre in prereq[course]:
if dfs(pre) == False:
return False
cycle.remove(course)
visit.add(course)
output.append(course)
return True
for c in range(numCourses):
if dfs(c) == False:
return []
return output
if __name__ == '__main__':
numCourses = 2
prerequisites = [[1, 0]]
print(Solution().findOrder(numCourses, prerequisites))