leetcode-1136-Parallel Courses
问题
There are N
courses, labelled from 1 to N
.
We are given relations[i] = [X, Y]
, representing a prerequisite relationship between course X
and course Y
: course X
has to be studied before course Y
.
In one semester you can study any number of courses as long as you have studied all the prerequisites for the course you are studying.
Return the minimum number of semesters needed to study all courses. If there is no way to study all the courses, return -1
.
Example 1:
1 | Input: N = 3, relations = [[1,3],[2,3]] |
Example 2:
1 | Input: N = 3, relations = [[1,2],[2,3],[3,1]] |
Note:
1 <= N <= 5000
1 <= relations.length <= 5000
relations[i][0] != relations[i][1]
- There are no repeated relations in the input.
分析
拓扑排序
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/30/leetcode-1136/
License: 知识共享署名-非商业性使用 4.0 国际许可协议