A college offers numCourses courses, numbered from 0 to numCourses - 1. The list prerequisites holds pairs [a, b], and each pair means: you must finish course b before you can take course a.
Return one valid order in which you can take all the courses. If the courses can never all be finished, return an empty array.
Which order to return: many orders can be valid, but the judge compares your answer with one fixed answer. So always take the smallest course number among the courses you can take next. (Use a min-heap in Kahn's algorithm.)
Examples
Example 1:
Input: numCourses = 3, prerequisites = [[2,1],[1,0]]
Output: [0,1,2]
Explanation: The courses form a chain: 0, then 1, then 2.
Example 2:
Input: numCourses = 4, prerequisites = [[0,3],[1,3]]
Output: [2,3,0,1]
Explanation: Courses 2 and 3 have no prerequisites. Take the smaller one, 2, first. Then 3. After 3, both 0 and 1 are free, and 0 is smaller.
Example 3:
Input: numCourses = 3, prerequisites = [[0,2],[2,1],[1,0]]
Output: []
Explanation: The three courses wait for each other in a circle, so no order exists.