-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_schedule.py
27 lines (24 loc) · 1 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
'''
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.
'''
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
todo = {i: set() for i in range(numCourses)}
graph = defaultdict(set)
for i,j in prerequisites:
todo[i].add(j)
graph[j].add(i)
q = deque([])
for k,v in todo.items():
if len(v) == 0:
q.append(k)
while q:
n = q.popleft()
for i in graph[n]:
todo[i].remove(n)
if len(todo[i]) == 0:
q.append(i)
todo.pop(n)
return not todo