Course Schedule Feasibility II (Variant)

Given numCourses and prerequisite pairs, return true if it is possible to finish all courses. Pattern focus: DFS Topological Order. This version emphasizes the recursive postorder approach and cycle detection via visit states.

Input Format

numCourses = course count, prerequisites = prerequisite pairs [course, prerequisite]

Output Format

true if all courses can be completed

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9

Examples

Example 1:

Input:

numCourses = 2
prerequisites = [[1,0]]

Output:

true

Explanation:

No cycle exists.

Example 2:

Input:

numCourses = 2
prerequisites = [[1,0],[0,1]]

Output:

false

Explanation:

The two courses depend on each other, which creates a cycle.

Example 3:

Input:

numCourses = 4
prerequisites = [[1,0],[2,1],[3,2]]

Output:

true

Explanation:

A chain without a back-edge is valid.

Loading...
Course Schedule Feasibility II (Variant)