Course Schedule II - Problem
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
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
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
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
โ Linear Growth
Space Complexity
O(V + E)
Space for adjacency list O(E), in-degree array O(V), and queue O(V)
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code