-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207.py
More file actions
27 lines (20 loc) · 763 Bytes
/
Copy path207.py
File metadata and controls
27 lines (20 loc) · 763 Bytes
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
# Course Schedule
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
adj=[[] for _ in range(numCourses)]
for course, pre in prerequisites:
adj[pre].append(course)
vis=[False]*numCourses
path=[False]*numCourses
def dfs(node):
vis[node] = path[node]=True
for next_node in adj[node]:
if not vis[next_node]:
if dfs(next_node): return True
elif path[next_node]: return True
path[node]=False
return False
for i in range(numCourses):
if not vis[i]:
if dfs(i): return False
return True