forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourse Schedule.py
29 lines (26 loc) · 1.01 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
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
pre = {} # course: list of prerequisites
dep = {} # course: list of dependents
for p in prerequisites:
if p[0] not in pre:
pre[p[0]] = set()
if p[1] not in dep:
dep[p[1]] = set()
pre[p[0]].add(p[1])
dep[p[1]].add(p[0])
# Kahn's algorithm
l = []
s = set()
for i in range(numCourses):
if i not in dep: # if no dependents (incoming edge)
s.add(i)
while s:
n = s.pop()
l.append(n)
if n in pre: # if n has prerequisites
for m in pre[n]: # for each prerequisites m
dep[m].remove(n) # remove n from m's dependents list
if not dep[m]: # if m has no more dependents
s.add(m)
return len(l) == numCourses