coding•DSA & Problem Solving•Graphs & Backtracking
Course Schedule
Problem Statement
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`.
### Constraints
- `1 <= numCourses <= 2000`
- `0 <= prerequisites.length <= 5000`
- `prerequisites[i].length == 2`
- `0 <= a_i, b_i < numCourses`
- All the pairs `prerequisites[i]` are unique.
### Input Format
- Line 1: Two space-separated integers `numCourses` and `m` (number of prerequisites).
- Next `m` lines: Two space-separated integers `a b`.
### Output Format
Print `true` or `false`.
Sample Cases
Example 1Sample Case
Input: 2 1
1 0
Output: true
Example 2Sample Case
Input: 2 2
1 0
0 1
Output: false
Example 3Sample Case
Input: 1 0
2 1
Output: true
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.