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, b]` indicates that you must take course `b` first if you want to take course `a`.
Return `true` if you can finish all courses, or `false` if a cycle exists making it impossible.
### Constraints
- `1 <= numCourses <= 2000`
- `0 <= prerequisites.length <= 5000`
- `prerequisites[i].length == 2`
- All pairs `[a, b]` are unique.
### Input Format
First line contains integer `numCourses`.
Second line contains integer `numPrerequisites`.
Next lines contain two space-separated integers `a b` representing each prerequisite pair.
### Output Format
Print `true` if all courses can be finished, otherwise `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: 4
3
1 0
2 1
3 2
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.