← All problems

Course Schedule

Medium graphstopological-sortbfs LeetCode ↗
There are n courses labeled 0..n-1 with prerequisite pairs [course, prerequisite]. Determine whether it is possible to finish all courses — equivalently, whether the prerequisite graph has a cycle.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n+e), Space O(n+e)

Common pitfalls

  • Edge direction flipped
  • DFS color method: forgetting the 'visiting' state

Your solution

Log in to save your code and track progress.