Course order
Mediumgraphtopological sortheap
There are n courses 0 to n-1. Each [a, b] in prereqs means b must come before a. Return an order that takes every course. If several orders work, return the lexicographically smallest (always take the smallest available course number). If impossible (a cycle), return [].
Example 1
Input: n = 4, prereqs = [[1,0],[2,0],[3,1],[3,2]] Output: [0,1,2,3]
Example 2
Input: n = 2, prereqs = [[0,1],[1,0]] Output: []
Example 3
Input: n = 3, prereqs = [] Output: [0,1,2]
Constraints
- 1 ≤ n ≤ 2000
- 0 ≤ prereqs.length ≤ 5000
- No repeated pairs
Submit checks your solution against 9 tests, including hidden ones.
/**
* @param {int} n
* @param {int[][]} prereqs
* @return {int[]}
*/
function findOrder(n, prereqs) {
}