Course Schedule - Problem
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
You are given an array prerequisites where prerequisites[i] = [a_i, b_i] indicates that you must take course b_i first if you want to take course a_i.
For example, the pair [0, 1] indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
Input & Output
Example 1 — Basic Valid Case
$
Input:
numCourses = 2, prerequisites = [[1,0]]
›
Output:
true
💡 Note:
Course 1 requires course 0 first. Valid order: take course 0, then course 1. No circular dependency.
Example 2 — Circular Dependency
$
Input:
numCourses = 2, prerequisites = [[1,0],[0,1]]
›
Output:
false
💡 Note:
Course 1 requires course 0, but course 0 requires course 1. This creates a cycle, making it impossible to complete all courses.
Example 3 — No Prerequisites
$
Input:
numCourses = 3, prerequisites = []
›
Output:
true
💡 Note:
No prerequisites means all courses can be taken in any order. Always possible to complete.
Constraints
- 1 ≤ numCourses ≤ 105
- 0 ≤ prerequisites.length ≤ 5000
- prerequisites[i].length == 2
- 0 ≤ ai, bi < numCourses
Visualization
Tap to expand
Understanding the Visualization
1
Input
numCourses=2, prerequisites=[[1,0]] - course 1 needs course 0
2
Process
Build graph and check for cycles using DFS or topological sort
3
Output
Return true if no cycles (can complete), false if cycles exist
Key Takeaway
🎯 Key Insight: Course scheduling is cycle detection in a directed graph - cycles make completion impossible
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code