Course Schedule
You now have numCourses courses that need to be taken, labeled from 0 to numCourses - 1. You are given an array prerequisites, where prerequisites[i] = [ai, bi] indicates that to take course ai, you must first complete course bi.
For example, if you want to study course 0, you need to finish course 1 first, and we can represent it with a pair [0,1].
Return one valid order of courses so that all courses can be completed in the required order.
Example
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
This problem is a classic topological sorting task on course prerequisites. Each course is a node, and each prerequisite pair forms a directed edge from the prerequisite course to the dependent course. The goal is to return one valid order that allows all courses to be completed; if a cycle exists, no valid ordering is possible. The most common solution is Kahn’s BFS algorithm using indegrees and a queue, though DFS with cycle detection also works. For the sample <code>numCourses = 4</code> and <code>prerequisites = [[1,0],[2,0],[3,1],[3,2]]</code>, one valid order is <code>[0,2,1,3]</code>.