BugForge
DSA

Minimum Truck Capacity

Mediumbinary search on answer 00:00

Packages must ship in the given order within days days. Each day one truck loads consecutive packages as long as total weight ≤ capacity. Return the minimum capacity that ships everything on time.

Signature

minCapacity(weights: int[], days: int) → int
Example 1
weights = [1,2,3,4,5,6,7,8,9,10]
days = 5
output = 15
capacity 15: [1..5][6,7][8][9][10]
Example 2
weights = [3,2,2,4,1,4]
days = 3
output = 6
capacity 6

Constraints

  • 1 ≤ days ≤ n ≤ 5·10^4
  • 1 ≤ weights[i] ≤ 500
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).