Capacity To Ship Packages Within D Days

Medium

A conveyor belt has packages that must be shipped from one port to another within D days. The i-th 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 D days.

Constraints

  • 1 <= D <= weights.length <= 5 * 10^4
  • 1 <= weights[i] <= 500

Example

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

Expected Output: 15

Edge Cases

  • If D = 1, the ship must carry all packages in one day, so capacity = sum of all weights.
  • If D >= number of packages, each package can be shipped separately, so capacity = max weight.