Description
There are numCourses courses labeled from 0 to numCourses - 1. Given prerequisites array, return the ordering of courses you should take to finish all courses. If impossible, return an empty array.
Examples
Input:
numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]Output:
[0,1,2,3]Explanation:
One valid ordering.
Input:
numCourses = 1, prerequisites = []Output:
[0]Explanation:
Edge case with minimal input.
Input:
numCourses = 3, prerequisites = [[1,0],[0,2],[2,1]]Output:
[]Explanation:
This creates a circular dependency: course 0 depends on 2, course 2 depends on 1, and course 1 depends on 0. Since there's a cycle in the prerequisite graph, it's impossible to complete all courses, so returning an empty array.
Constraints
- •
1 ≤ numCourses ≤ 2000 - •
0 ≤ prerequisites.length ≤ numCourses * (numCourses - 1)