-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_schedule.py
More file actions
43 lines (32 loc) · 1.13 KB
/
Copy pathcourse_schedule.py
File metadata and controls
43 lines (32 loc) · 1.13 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
LC 207. Course Schedule
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
You are given an array prerequisites where prerequisites[i] = [ai, bi]
indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
"""
def canFinish(numCourses: int, prerequisites: list[list[int]]) -> bool:
g = [[] for _ in range(numCourses)]
for course, prereq in prerequisites:
g[prereq].append(course)
state = [0] * numCourses
def dfs(u: int) -> bool:
if state[u] == 1:
return False
if state[u] == 2:
return True
state[u] = 1
for v in g[u]:
if not dfs(v):
return False
state[u] = 2
return True
for i in range(numCourses):
if state[i] == 0:
if not dfs(i):
return False
return True
numCourses = 2
prerequisites = [[1, 0], [0, 1]]
print(canFinish(numCourses, prerequisites))