-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathprerequisites.py
30 lines (28 loc) · 1.04 KB
/
prerequisites.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
from collections import defaultdict, deque
from typing import List
def prerequisites(n: int, prerequisites: List[List[int]]) -> bool:
graph = defaultdict(list)
in_degrees = [0] * n
# Represent the graph as an adjacency list and record the in-
# degree of each course.
for prerequisite, course in prerequisites:
graph[prerequisite].append(course)
in_degrees[course] += 1
queue = deque()
# Add all courses with an in-degree of 0 to the queue.
for i in range(n):
if in_degrees[i] == 0:
queue.append(i)
enrolled_courses = 0
# Perform topological sort.
while queue:
node = queue.popleft()
enrolled_courses += 1
for neighbor in graph[node]:
in_degrees[neighbor] -= 1
# If the in-degree of a neighboring course becomes 0, add
# it to the queue.
if in_degrees[neighbor] == 0:
queue.append(neighbor)
# Return true if we've successfully enrolled in all courses.
return enrolled_courses == n