← All problems

Course Schedule II (return ordering)

Medium graphstopological-sort LeetCode ↗
Given course prerequisites as [course, prerequisite] pairs, return a valid order in which to take all n courses, or an empty array if no such order exists (i.e., the 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

  • Returning partial order on cycle (must return [])
  • See previous pitfalls

Your solution

Log in to save your code and track progress.