Given the number of courses and prerequisite pairs, return an order you can take to finish every course.
If no valid ordering exists, return an empty array.
Model the relationships explicitly, then choose BFS, DFS, or union find based on the question. Track visited state so cycles do not create repeated work.
Examples
Input: numCourses = 2, prerequisites = [[1,0]] Output: [0,1]
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,1,2,3]
Constraints
- 1 <= numCourses <= 2000
- 0 <= prerequisites.length <= numCourses * (numCourses - 1)