Capacity To Ship Packages Within D Days
Medium
Topics
Package weights nums must be shipped in order within target days. Each day loads a contiguous prefix whose total weight is at most the ship capacity. Return the minimum capacity that ships everything within target days.
Example 1
Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5 Output: 15
Example 2
Input: weights = [3,2,2,4,1,4], days = 3 Output: 6
Constraints
- 1 <= nums.length <= 5*10^4
- 1 <= target <= nums.length
- 1 <= nums[i] <= 500
Editorial — intuition, approach, complexity and pitfalls
Intuition
Package order is fixed, so a capacity determines the shipping plan entirely: load greedily until the next package would overflow, then start a new day. That makes the number of days a function of capacity — and a monotonically non-increasing one, since a bigger ship never needs more days.
Monotonic means binary searchable. Rather than searching for the plan, search for the capacity: the smallest value whose greedy plan fits within the day limit.
The bounds are what make the search correct. The lower bound is max(weights) — any smaller ship cannot carry the heaviest package at all. The upper bound is sum(weights), one day for everything.
💡 Same pattern as "divide chocolate" and "split array largest sum": binary search on the answer, with a linear greedy feasibility check per probe. Recognise it whenever the task is to minimise a maximum or maximise a minimum.
Approach
lo = max(weights),hi = sum(weights).- For each
mid, greedily count days; if it fits the limit, search lower, else higher. - The loop shape
while lo < hiwithhi = midconverges on the smallest feasible capacity.
Dry Run
weights = [1,2,3,4,5,6,7,8,9,10], days = 5
lo = 10, hi = 55
mid=32 plan: [1..8]=36>32 so [1..7]=28, then [8,9,10]=27
-> 2 days <= 5 feasible, hi=32
mid=21 [1..6]=21, [7,8]=15, [9,10]=19 -> 3 days feasible, hi=21
mid=15 [1,2,3,4,5]=15, [6,7]=13, [8]=8... -> 5 days feasible, hi=15
mid=12 [1,2,3,4]=10, [5,6]=11, [7]=7, [8]=8, [9]=9, [10]=10 -> 6 days
not feasible, lo=13
mid=14 -> 5 days feasible, hi=14
mid=13 -> 6 days not feasible, lo=14
lo == hi == 15? converges to 15
answer 15Complexity
- Time — O(n log(sum)): each feasibility check is one linear pass, and the search halves a range bounded by the total weight. At the limits that is about 25 probes over 5*10^4 packages.
- Space — O(1): the greedy check allocates nothing.
Common Pitfalls
- Starting
loat 1 rather thanmax(weights). The search still converges, but every probe below the heaviest package is infeasible for a reason the greedy check must then discover. - Writing the greedy to split when the running load reaches capacity rather than exceeds it, which wastes a day.
- Mixing loop shapes:
while lo < hipairs withhi = mid. Usinghi = mid - 1here can skip the answer. - Searching over the number of days instead of the capacity. Days are the constraint, not the unknown.
Full Solution — all languages
class Solution:
def shipWithinDays(self, nums, target):
def ok(cap):
d, cur = 1, 0
for w in nums:
if cur + w > cap:
d += 1
cur = 0
cur += w
return d <= target
lo, hi = max(nums), sum(nums)
while lo < hi:
mid = (lo + hi) // 2
if ok(mid):
hi = mid
else:
lo = mid + 1
return loimport java.util.*;
class Solution {
public int shipWithinDays(int[] nums, int target) {
int lo = 0, hi = 0;
for (int w : nums) {
lo = Math.max(lo, w);
hi += w;
}
while (lo < hi) {
int mid = (lo + hi) / 2;
if (ok(nums, target, mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
boolean ok(int[] nums, int target, int cap) {
int d = 1, cur = 0;
for (int w : nums) {
if (cur + w > cap) {
d++;
cur = 0;
}
cur += w;
}
return d <= target;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int shipWithinDays(vector<int>& nums, int target) {
int lo = 0, hi = 0;
for (int w : nums) {
lo = max(lo, w);
hi += w;
}
auto ok = [&](int cap) {
int d = 1, cur = 0;
for (int w : nums) {
if (cur + w > cap) {
d++;
cur = 0;
}
cur += w;
}
return d <= target;
};
while (lo < hi) {
int mid = (lo + hi) / 2;
if (ok(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var shipWithinDays = function(nums, target) {
let lo = 0, hi = 0;
for (const w of nums) {
lo = Math.max(lo, w);
hi += w;
}
const ok = (cap) => {
let d = 1, cur = 0;
for (const w of nums) {
if (cur + w > cap) {
d++;
cur = 0;
}
cur += w;
}
return d <= target;
};
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (ok(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
};