Skip to content
Onsite

Course Schedule

MediumP1Coding

There are numCourses courses labelled 0 to numCourses - 1. Each entry [a, b] in prerequisites means you must take course b before course a.

Return true if you can finish all courses, otherwise false. This is the same as asking whether the prerequisite graph has no cycle.

Example 1

Input
numCourses = 2, prerequisites = [[1,0]]
Output
true
Why
Take 0, then 1.

Example 2

Input
numCourses = 2, prerequisites = [[1,0],[0,1]]
Output
false
Why
Each course needs the other first.

Constraints

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • 0 <= a, b < numCourses
  • Duplicate pairs may appear.

Hints

Run checks the 3 visible tests, Submit all 11.