Capacity To Ship Packages Within D Days
Problem Statement
Packages sit on a conveyor belt in a fixed order. Package i weighs weights[i]. They must all be shipped from one port to another within days days.
Each day the ship is loaded with packages in belt order, starting from the next unshipped package. The total weight loaded on one day can't go over the ship's capacity. You can't skip a package or change the order.
Return the smallest ship capacity that ships every package within days days.
An integer array weights and an integer days.
One integer: the minimum ship capacity.
With capacity 15 the days are [1,2,3,4,5], [6,7], [8], [9], [10]. With capacity 14 you need 6 days.
With capacity 6 the days are [3,2], [2,4], [1,4].
With capacity 3 the days are [1,2], [3], [1,1]. That is only 3 days, which is allowed (at most 4).
- • 1 ≤ days ≤ weights.length ≤ 5 · 10^4
- • 1 ≤ weights[i] ≤ 500
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try capacity max, max+1, … and return the first that fits in `days` days
Binary search the range [max, sum]; test each guess with a greedy day count
The answer is at least the heaviest package. So try capacities max, max + 1, max + 2, …. For each one, load the ship greedily and count the days. The first capacity that needs at most days days is the answer.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.