There are course_count courses numbered from zero through course_count - 1. Each pair [course, prerequisite] says that the prerequisite must be completed before the course. Determine whether it is possible to complete all courses.
The named course-scheduling exercise is interpreted here as feasibility under prerequisite dependencies. Return a Boolean rather than a particular course order.
Function Signature
def can_complete_courses(course_count: int, prerequisites: list[list[int]]) -> bool:
Rules
-
Every listed prerequisite must be satisfied.
-
Courses without prerequisites may be taken immediately.
-
Duplicate pairs repeat the same requirement and do not change feasibility.
-
A course requiring itself makes completion impossible.
-
Zero courses can be completed trivially, so return
True
.
Constraints
These bounds are exercise assumptions.
-
0 <= course_count <= 100000
.
-
0 <= len(prerequisites) <= 200000
.
-
Each pair contains two integers in
[0, course_count - 1]
.
-
An empty course set has no prerequisite pairs.
Examples
Input: course_count = 4, prerequisites = [[1, 0], [2, 1], [3, 1]]
Output: True
Course 0 can precede 1, after which both 2 and 3 are available.
Input: course_count = 2, prerequisites = [[1, 0], [0, 1]]
Output: False
Each course waits for the other, so neither can be started.