FastPrepCapacity To Ship Packages Within D Days

Capacity To Ship Packages Within D Days

Amazon logoAmazon● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Problem statement

A conveyor belt has packages that must be shipped from one port to another within days days.

The i-th package has weight weights[i]. Packages are loaded in the given order: you may not rearrange them. Each day, you load the conveyor with packages whose total weight does not exceed the ship's capacity, then ship that day's load.

Return the least ship capacity that can move 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 as [1,2,3,4,5], [6,7], [8], [9], and [10]. Capacity 14 needs more than 5 days.

Example 2

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

Capacity 6 ships as [3,2], [2,4], and [1,4].

Constraints

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

More Amazon problems

See Amazon hiring insights
Saved
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…