Skip to content
Problem 207 · LeetCode 207(opens in a new tab)

Course Schedule

MediumGraphs

You have numCourses courses, numbered from 0 up to numCourses − 1. Some of them have a prerequisite: the pair [a, b] in prerequisites says course b must be finished before course a can start.

Return true if there is some order that lets you finish every course, and false if no such order exists.

Example 1

Input:  numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: true
Why:    take 0, then 1 and 2, then 3
2 more examples and the constraints

Example 2

Input:  numCourses = 4, prerequisites = [[1,0],[2,1],[3,2],[1,3]]
Output: false
Why:    1 waits on 3, 3 on 2, and 2 on 1: a loop

Example 3

Input:  numCourses = 3, prerequisites = []
Output: true
Why:    no rules, so any order works

Constraints

  • 1 ≤ numCourses ≤ 2,000
  • 0 ≤ number of pairs ≤ 5,000
  • Both numbers in a pair are valid course numbers
  • No pair is listed twice

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Search from every courseO(V·(V + E))
  2. DFS with three statesO(V + E)
  3. Take ready courses (Kahn's BFS)O(V + E)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 2,000 courses and 5,000 pairsVerdict
Search from every courseO(V·(V + E))
Mention it, then move on
DFS with three statesO(V + E)
Passes; watch recursion depth
Kahn's BFSO(V + E)
Best; no recursion, easy to explain

V = number of courses, E = number of prerequisite pairs.

Edge cases to test

  • No prerequisites. An empty list means any order works: return true.
  • Two courses that need each other. [[0,1],[1,0]] is the smallest loop between different courses.
  • A course that needs itself. [[1,1]] is a loop of one; it can never be taken.
  • A loop in one part only. Separate groups of courses must all be checked; one loop anywhere makes the answer false.
  • Two paths into one course. Reaching a course twice is not a loop, as with course 3 in Example 1.

Common mistakes

  • Using one visited set in DFS, so a course reached by two different paths looks like a loop.
  • Searching only from course 0 and missing a loop in a part of the graph it cannot reach.
  • Drawing the arrows the wrong way round. It still gives the right answer here, but it breaks when the question asks for the order.

How to talk through it

  1. 1Clarify. Can a pair repeat? Can a course list itself? Do I return an order or only yes or no?
  2. 2Reframe as a graph. Courses are nodes, each pair is an arrow. An order exists exactly when there is no loop.
  3. 3Give the brute force. Search from every course to see if it comes back to itself; say why that repeats work.
  4. 4Code Kahn's algorithm. Count prerequisites, queue the free courses, cross out arrows as you take them, and compare the count taken with numCourses.
  5. 5Test out loud. Run the empty list, the two-course loop and the diamond with a shared course before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?