Capacity To Ship Packages Within D Days

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

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

The i-th package has a weight of weights[i]. Each day, we load packages onto the ship in the order given. 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 being shipped within days days.

Examples

Example 1:

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

Output: 15

Explanation: A capacity of 15 ships packages in 5 days: [1,2,3,4,5], [6,7], [8], [9], [10].

Example 2:

Input: weights = [3, 2, 2, 4, 1, 4], days = 3

Output: 6

Example 3:

Input: weights = [1, 2, 3, 1, 1], days = 4

Output: 3

Constraints

  • 1 <= days <= weights.length <= 5 * 10^4
  • 1 <= weights[i] <= 500
Source: Modified Binary Search pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle