Ctrl K

Smallest Ferry for the Schedule

Medium
Crates are lined up on a pier, and crate i weighs crates[i]. A ferry makes one trip a day and takes the crates strictly in line order: it loads them one after another until the next crate would overload it, then sails. Crates cannot be reordered or skipped. Find the smallest ferry capacity (an integer) that gets all the crates across in at most days days.
Example 1
crates=[4, 8, 2, 6, 5, 3], days=3 → 12

With capacity 12: day 1 carries 4 + 8, day 2 carries 2 + 6, day 3 carries 5 + 3. With 11 it takes four trips: 4 | 8 + 2 | 6 + 5 | 3.

Example 2
crates=[7, 1, 1, 1], days=4 → 7

The heaviest crate weighs 7, so the ferry cannot be smaller, and with 7 two days are already enough.

Example 3
crates=[3, 3, 3], days=1 → 9

Everything has to go in one trip: 3 + 3 + 3 = 9.

Constraints
  • 1 ≤ days ≤ len(crates) ≤ 5 · 10⁴
  • 1 ≤ crates[i] ≤ 500
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFind in a Shifted Sorted Array MediumFirst and Last Copy MediumFind in a Row-Chained Grid MediumFind in a Shifted Array With Repeats MediumMinimum of a Shifted List Medium
loading Python…
Input
crates=[4, 8, 2, 6, 5, 3], days=3
Expected
12

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