P10 · 2 seconds · 256 MB

Capacity to Ship Packages

A conveyor holds packages in a fixed order with weights `weights[i]`. You must ship them in at most `days` days. Each day you load a prefix of the remaining packages whose total weight is at most capacity `c`. You cannot reorder packages and you cannot split a package. Return the smallest integer capacity `c` that lets you finish in at most `days` days.

function shipWithinDays(weights, days) → number

CONSTRAINTS

  • 1 ≤ weights.length ≤ 10⁴
  • 1 ≤ days ≤ weights.length
  • 1 ≤ weights[i] ≤ 10⁵
Input: weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
Output: 15
One feasible loading is 1+2+3+4+5, then 6+7, then 8, then 9, then 10. Capacity 14 needs six days.
Input: weights = [3, 2, 2, 4, 1, 4], days = 3
Output: 6
Input: weights = [1, 2, 3, 1, 1], days = 4
Output: 3
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.