Skip to main content
Back to problems
#1011
Medium Algorithms

Capacity to ship packages within d days

Array Binary Search
73.5% acceptance
Feb 25, 2026
10919
286
A conveyor belt has packages that must be shipped from one port to another within days days. The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship. Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn ship_within_days(weights: Vec<i32>, days: i32) -> i32 {
    let (mut lo, mut hi) = (*weights.iter().max().unwrap(), weights.iter().sum::<i32>());
    while lo < hi {
      let mid = (lo + hi) / 2;
      let mut need = 1; let mut cur = 0;
      for &w in &weights { if cur + w > mid { need += 1; cur = 0; } cur += w; }
      if need <= days { hi = mid; } else { lo = mid + 1; }
    }
    lo
  }
}