You must finish n courses labeled 0 through n - 1. Each pair [a, b] means course b must be completed before course a. Return whether a completion order exists for every course.
Function Signature
def courses_are_possible(n: int, prerequisites: list[list[int]]) -> bool:
Rules
-
Every dependency is mandatory, including those in disconnected parts of the graph.
-
Repeated pairs have the same effect as one occurrence.
-
A course may appear as its own prerequisite. Such an input is impossible to complete.
-
Return
True
for zero courses.
Constraints
These limits are exercise assumptions for the referenced prerequisite problem.
-
0 <= n <= 100000
.
-
0 <= len(prerequisites) <= 200000
.
-
Each pair contains exactly two integers from
0
through
n - 1
.
-
When
n == 0
, the prerequisite list is empty.
Examples
Input: n = 3, prerequisites = [[2, 1], [1, 0]]
Output: True
Courses can be completed in the order zero, one, two.
Input: n = 4, prerequisites = [[1, 0], [2, 3], [3, 2]]
Output: False
Courses zero and one can be completed, but courses two and three form a cycle.