-
Notifications
You must be signed in to change notification settings - Fork 0
/
207-Course_Schedule.py
33 lines (25 loc) · 1001 Bytes
/
207-Course_Schedule.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
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
course_col = collections.defaultdict(list)
for realate in prerequisites:
nex, pre = realate[0], realate[1]
course_col[ pre ].append(nex)
checked = [False]*numCourses
path = [False]*numCourses
def isCyclic( currCourse ):
if checked[ currCourse ]:
return False
if path[ currCourse ]:
return True
path[ currCourse ] = True
k = False
for child in course_col[ currCourse ]:
k = isCyclic( child )
if k: break
path[ currCourse ] = False
checked[ currCourse ] = True
return k
for numC in range(numCourses):
if isCyclic(numC):
return False
return True