FastPrepCapacity to Ship Packages Within Days

Capacity to Ship Packages Within Days

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Packages must be shipped in the order given by weights. Each day, load a consecutive prefix of the remaining packages without exceeding the ship's capacity.

Return the smallest positive capacity that ships every package within days days.

Function

shipWithinDays(weights: int[], days: int) → int

Examples

Example 1

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

Capacity 15 ships the ordered packages in five days, while any smaller capacity needs more days.

Example 2

weights = [3,2,2,4,1,4]days = 3return = 6

The loads can be [3,2], [2,4], and [1,4].

Example 3

weights = [1,2,3,1,1]days = 4return = 3

Capacity 3 is enough and no capacity below the heaviest package can work.

Constraints

  • 1 <= weights.length <= 50000.
  • 1 <= weights[i] <= 500.
  • 1 <= days <= weights.length.

More Microsoft problems

See Microsoft hiring insights
public int shipWithinDays(int[] weights, int days) {
    // Write your code here.
}
weights[1,2,3,4,5,6,7,8,9,10]
days5
expected15
Checking account…