LeetCode 210 Course Schedule II - Problem Statement

The problem

Given numCourses and prerequisites, return an ordering of courses to finish all of them. If impossible, return an empty array.

With numCourses = 4 and prerequisites = [[1,0],[2,0],[3,1],[3,2]]:

  • Course 0 has no prereqs. Take it first.
  • Courses 1 and 2 need 0. Take them next (either order).
  • Course 3 needs 1 and 2. Take it last.
  • Valid ordering: [0, 1, 2, 3] or [0, 2, 1, 3].

With numCourses = 2 and prerequisites = [[1,0],[0,1]]:

  • Circular dependency. Return [].

Constraints: 1numCourses20001 \le numCourses \le 2000.