-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcourse_schedule.py
46 lines (32 loc) · 1.07 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
# 207. Course Schedule
# https://leetcode.com/problems/course-schedule/
from collections import defaultdict
from typing import List
class Solution:
def dfs(self, node, graph, visited):
if node not in graph:
return True
for n in graph[node]:
if n in visited:
return visited[n] == 2
visited[n] = 1
if not self.dfs(n, graph, visited):
return False
visited[n] = 2
return True
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
graph = defaultdict(list)
for p in prerequisites:
graph[p[1]].append(p[0])
visited = defaultdict(int)
for node in graph:
visited[node] = 1
if not self.dfs(node, graph, visited):
return False
visited[node] = 2
return True
if __name__ == '__main__':
sol = Solution()
print(sol.canFinish(2, [[1, 0]]))
print(sol.canFinish(2, [[1, 0], [0, 1]]))
print(sol.canFinish(3, [[1, 0], [3, 1], [3, 2]]))