-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanFinish.py
More file actions
22 lines (21 loc) · 772 Bytes
/
canFinish.py
File metadata and controls
22 lines (21 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
indegree = [0] * numCourses
adjacent = {}
for course,pre in prerequisites:
indegree[course] += 1
adjacent[pre] = adjacent.get(pre,[]) + [course]
queue = deque()
for i in range(len(indegree)):
if indegree[i] == 0:
queue.append(i)
res = []
while queue:
course = queue.popleft()
res.append(course)
adjacents = adjacent.get(course,[])
for adj in adjacents:
indegree[adj] -= 1
if indegree[adj] == 0:
queue.append(adj)
return len(res) == numCourses