Course Schedule

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 true if it is possible to finish every course, and false otherwise.

Constraints

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • 0 <= a, b < numCourses
  • A pair can appear more than once, and a can equal b (a course that requires itself)

Examples

Example 1

Input: numCourses = 4, prerequisites = [[1,0],[2,1],[3,2]]
Output: true

Take 0, then 1, then 2, then 3. Each course's only prerequisite is the one before it.

Example 2

Input: numCourses = 3, prerequisites = [[0,1],[1,2],[2,0]]
Output: false

0 needs 1, 1 needs 2 and 2 needs 0: a loop. None of them can ever be the first one taken.

Example 3

Input: numCourses = 5, prerequisites = []
Output: true

No prerequisites at all: take the courses in any order.

JavaScript you'll use: Graphs and grids in JavaScript Stacks, queues and deques

/**
 * @param {number} numCourses
 * @param {number[][]} prerequisites
 * @return {boolean}
 */
function canFinish(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.