Minimum Semesters With Course Capacity
Updated
Problem statement
There are n courses labeled from 1 through n. Each pair [a, b] in prerequisites means course a must be finished before course b.
In one semester, you may take at most maxCourses courses. You can take a course only if all of its prerequisites were finished in earlier semesters. Return the minimum number of semesters needed to finish every course.
No chain of prerequisites ever loops back to a course it started from. When more than maxCourses courses are ready to take, choose the group that leads to the smallest possible number of semesters overall. Do not rely on a fixed rule for which ready courses to take first.
Function
minimumSemesters(n: int, prerequisites: int[][], maxCourses: int) → intExamples
Example 1
n = 4prerequisites = [[1,3],[2,3],[3,4]]maxCourses = 2return = 3Courses 1 and 2 have no prerequisites, so take both in the first semester. Then take course 3, then course 4, for three semesters in all.
Example 2
n = 4prerequisites = []maxCourses = 2return = 2With no prerequisites, take any two courses in each semester, so all four courses take two semesters.
Constraints
1 <= n <= 150 <= prerequisites.length <= n * (n - 1) / 21 <= maxCourses <= n- Every prerequisite pair contains two different course labels in
[1, n]. - No pair appears twice, and no chain of prerequisites loops back to a course it started from.