Ctrl K

Study Plan With a Load Cap

Hard
A program has count courses numbered 1 to count. A pair [a, b] in prereqs means course a must be completed before course b can be taken. The prerequisites contain no cycles. In one term you can take at most load courses, and all of their prerequisites must have been completed in earlier terms. Return the minimum number of terms needed to complete every course.
Example 1
count=5, prereqs=[[1, 3], [2, 3], [3, 4], [3, 5]], load=2 → 3

Term 1: courses 1 and 2. Term 2: course 3. Term 3: courses 4 and 5.

Example 2
count=6, prereqs=[[1, 2], [2, 3]], load=2 → 3

The chain 1 → 2 → 3 needs three terms, so course 1 must be taken right away: 1 and 4, then 2 and 5, then 3 and 6. Starting with 4 and 5 would take four terms.

Example 3
count=4, prereqs=[], load=4 → 1

There are no prerequisites, and all four courses fit into one term.

Constraints
  • 1 ≤ count ≤ 15
  • 1 ≤ load ≤ count
  • 0 ≤ len(prereqs) ≤ count · (count − 1) / 2
  • 1 ≤ a, b ≤ count, a ≠ b
  • All pairs in prereqs are distinct
  • The prerequisites contain no cycles
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Walk Through Every Node HardPriciest Toll Road Trip HardSettle Up in the Fewest Transfers HardTwo Groups With the Same Average HardAssembling Fragments Hard
loading Python…
Input
count=5, prereqs=[[1, 3], [2, 3], [3, 4], [3, 5]], load=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.