solve(numCourses, prerequisites) — same input as graph-course-schedule: each
pair [a, b] means course b must be taken before course a.
Return an order in which to take all the courses. To make the answer unique,
return the lexicographically smallest valid order: whenever several courses are
available, take the one with the smallest number. If the courses cannot all be
taken (a cycle), return [].
Examples
solve(4, [[1,0],[2,0],[3,1],[3,2]]) → [0,1,2,3]
solve(2, [[0,1]]) → [1,0]
solve(3, []) → [0,1,2]
solve(4, [[0,3],[1,3]]) → [2,3,0,1] // 2 is free from the start; 3 unlocks 0 and 1
solve(2, [[1,0],[0,1]]) → []Kahn's algorithm with a min-heap (or any structure that gives the smallest
ready course) produces exactly this order.
Sample tests