← 返回 bytedance 的题目列表Minimum Semesters to Finish All Courses
类型:online_judge
Given a course schedule with some courses having prerequisites, determine the minimum number of semesters required to complete all courses. You can take several courses in one semester as long as the prerequisites for each course are met. Provide the number of courses and the prerequisite relationships between them. Return the minimum number of semesters needed to finish all courses.
Input:
n: An integer representing the total number of courses.
prerequisites: A two-dimensional list where each element is a pair [a, b] indicating course a is a prerequisite for course b.
Output:
An integer representing the minimum number of semesters needed to complete all courses.
Example:
Input:
4
[[2,1],[3,1],[1,4]]
Output:
2
Constraints:
n is within the range [1, 5000]
The length of prerequisites does not exceed 5000
0 <= a, b < n
Example
Input
4
[[2,1],[3,1],[1,4]]