Minimum Ship Capacity

medium · binary-search, greedy

Minimum Ship Capacity

You are given an array weights where weights[i] is the weight of the i-th package. You must ship packages in order over days days. Each day you can ship a contiguous prefix of the remaining packages whose total weight is at most the ship's capacity.

Return the minimum ship capacity needed to ship all packages within days.

Function signature

func MinShipCapacity(weights []int, days int) int

Example

weights = [1,2,3,4,5,6,7,8,9,10]
days = 5
output = 15

Constraints

  • 1 <= len(weights) <= 200_000
  • 1 <= days <= len(weights)
  • 1 <= weights[i] <= 1_000_000

Notes

  • Feasibility is monotonic: if capacity X works, any larger capacity works.
  • Use binary search between max(weights) and sum(weights).
Run tests to see results
No issues detected