Course Schedule II
There are numCourses courses, numbered from 0 to numCourses - 1. Each pair [a, b] in prerequisites means course b must be finished before course a can be started.
Return an array containing every course exactly once, in an order in which they can all be taken: each course must appear after all of its prerequisites. If several orders work, return any of them. If it is impossible to finish every course, return an empty array [].
Constraints
1 <= numCourses <= 20000 <= prerequisites.length <= 50000 <= a, b < numCourses- A pair can appear more than once, and
acan equalb
Examples
Example 1
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,1,2,3]
0 has no prerequisites, so it goes first. 1 and 2 each need only 0, and 3 needs both. [0, 2, 1, 3] is just as correct.
Example 2
Input: numCourses = 2, prerequisites = [[0,1],[1,0]] Output: []
0 and 1 each need the other, so neither can be first: return [].
Example 3
Input: numCourses = 3, prerequisites = [] Output: [0,1,2]
With no prerequisites any order of 0, 1 and 2 works.
JavaScript you'll use: Graphs and grids in JavaScript Stacks, queues and deques
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {number[]}
*/
function findOrder(numCourses, prerequisites) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.