Course Schedule II is a classic graph problem that challenges you to find a valid ordering of courses based on their prerequisites.

You're a university student planning your academic journey! You have numCourses courses labeled from 0 to numCourses - 1, and an array of prerequisites where each prerequisites[i] = [a, b] means you must complete course b before taking course a.

Your goal is to return any valid ordering of courses that allows you to complete all of them. If it's impossible to finish all courses (due to circular dependencies), return an empty array.

For example, if you have courses [0, 1] with prerequisite [[0, 1]], you must take course 1 first, then course 0. The valid order would be [1, 0].

Input & Output

example_1.py โ€” Basic Course Schedule
$ Input: numCourses = 2, prerequisites = [[1,0]]
โ€บ Output: [0, 1]
๐Ÿ’ก Note: There are 2 courses. Course 1 depends on course 0, so we must take course 0 first, then course 1. One valid order is [0, 1].
example_2.py โ€” Multiple Prerequisites
$ Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
โ€บ Output: [0, 1, 2, 3] or [0, 2, 1, 3]
๐Ÿ’ก Note: Course 0 has no prerequisites. Courses 1 and 2 both depend on 0. Course 3 depends on both 1 and 2. Valid orders include [0,1,2,3] or [0,2,1,3].
example_3.py โ€” Circular Dependency
$ Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
โ€บ Output: []
๐Ÿ’ก Note: Course 1 requires course 0, and course 0 requires course 1. This creates a circular dependency, making it impossible to complete all courses.

Visualization

Tap to expand
๐ŸŽ“ Academic Course PlanningSemester 1: Foundation CoursesMath 101(No prereqs)Eng 101(No prereqs)Semester 2: Intermediate CoursesMath 201(Needs 101)Physics(Needs Math)Semester 3: Advanced CourseCalculus(Needs 201)โš ๏ธ Cycle DetectedCourse ACourse B
Understanding the Visualization
1
Map Dependencies
Create a prerequisite map showing which courses unlock other courses
2
Find Starting Courses
Identify courses with no prerequisites - these can be taken in the first semester
3
Plan Semester by Semester
Each semester, take available courses and see what new courses become available
4
Check for Impossible Situations
If we can't complete all courses, there must be circular dependencies
Key Takeaway
๐ŸŽฏ Key Insight: Course scheduling is topological sorting on a directed graph. Use in-degree counting (Kahn's algorithm) to process courses level by level, naturally detecting impossible circular dependencies.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(V + E)

Each course (vertex) and prerequisite (edge) is processed exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(V + E)

Space for adjacency list O(E), in-degree array O(V), and queue O(V)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค numCourses โ‰ค 2000
  • 0 โ‰ค prerequisites.length โ‰ค numCourses ร— (numCourses - 1)
  • prerequisites[i].length == 2
  • 0 โ‰ค ai, bi < numCourses
  • ai โ‰  bi
  • All the pairs [ai, bi] are distinct
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
68.0K Views
High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen