Course Schedule
Graph
medium
Score: 40
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] = [ai, bi]
indicates that you must take course bi
first if you want to take course ai
.
- For example, the pair
[0, 1]
, indicates that to take course0
you have to first take course1
.
Return 1
if you can finish all the courses else return 0
.
Input Format:
First Parameter - number numCourses
Second parameter - matrix prerequisites
of size m x n
Output Format:
Return 1
if you can finish all the courses else return 0
.
Example 1:
Input:
2
1 2
1 0
Output: 1
Explanation: 1 2 represents the size of the matrix. There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
2
2 2
1 0
0 1
output: 0
Explanation: 2 2 represents size of matrix .There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Constraints:
- 1 <= numCourses <= 2000
- 0 <= prerequisites.length <= 5000
- prerequisites[i].length == 2
- 0 <= ai, bi < numCourses
- Expected Time Complexity: O(m+n)
- Expected Auxiliary Space: O(m+n)
- All the pairs prerequisites[i] are unique