Course Schedule II
Same setup as Course Schedule: numCourses courses and prerequisite pairs [a, b] meaning course b must come before course a. This time, return an actual order in which all courses can be taken. Any valid order is accepted. If no valid order exists (the requirements are circular), return an empty array. Up to 2000 courses and 5000 pairs.
Examples
- Input:
- numCourses = 2, prerequisites = [[1,0]]
- Output:
- [0,1]
Course 1 requires course 0, so 0 must come first.
- Input:
- numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
- Output:
- [0,1,2,3]
Course 0 unlocks 1 and 2, both of which unlock 3. [0,2,1,3] would also be accepted.
- Input:
- numCourses = 2, prerequisites = [[1,0],[0,1]]
- Output:
- []
The two courses require each other, so no order works.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.