Skip to content
Decoder

Course Schedule

Medium

Problem

There are num_courses courses labelled 0 to num_courses - 1. Each pair [a, b] in prerequisites means you must finish course b before course a. Return true if you can finish every course, and false if the requirements make that impossible.

Example 1

Input
num_courses = 2, prerequisites = [[1, 0]]
Output
true

Example 2

Input
num_courses = 2, prerequisites = [[1, 0], [0, 1]]
Output
false
Why
each needs the other

Example 3

Input
num_courses = 3, prerequisites = []
Output
true

Constraints

  • 1 <= num_courses <= 2000
  • 0 <= prerequisites.length <= 5000
  • 0 <= a, b < num_courses

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.