#1494
Parallel Courses II
international master · 1965 · lc hard +32 · verified · 30.6% accepted · 1,134 likes · top 8%
Description
There are n courses (labeled 1 to n), and relations[i] = [prevCoursei, nextCoursei] states that prevCoursei must be completed before nextCoursei. Each semester you may take at most k courses, provided all their prerequisites were completed in earlier semesters. Return the minimum number of semesters required to complete all courses.
Example 1:
Input: n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
Output: 3
Explanation: The figure above represents the given graph.
In the first semester, you can take courses 2 and 3.
In the second semester, you can take course 1.
In the third semester, you can take course 4.
Example 2:
Input: n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output: 4
Explanation: The figure above represents the given graph.
In the first semester, you can only take courses 2 and 3 since you cannot take more than two per semester.
In the second semester, you can take course 4.
In the third semester, you can take course 1.
In the fourth semester, you can take course 5.
Code
1
2
3