There are a total of
n
courses you have to take, labeled from 0 to
n-1
. Some courses may have prerequisites, for example, to take course 3 you have to first take course 2, which is expressed as a pair:
[3, 2]
. Given the total number of courses and a list of prerequisite pairs,
is it possible for you to finish all the courses?
Problem Note
-
Return
1
if it is possible to finish all the courses, or0
if it is not possible to finish all the courses. - The input prerequisites is a graph represented by a list of edges, not adjacency matrices.
- You may assume that there are no duplicate edges in the input prerequisites.
Example 1
Input: n = 3
list of prerequisite pairs = [[1, 0], [2, 1], [3, 2]]
Output: 1
Explanation: There are a total of 3 courses to take. To pick course 1 you should have finished course 0, and to pick course 2 you should have finished course 1 and to pick course 3 you should have finished course 2. So it is possible.
Example 2:
Input: n = 2
list of prerequisite pairs = [[1,0],[0,1]]
Output: 0
Explanation: 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.