Skip to content
Decoder

Capacity To Ship Packages Within D Days

Medium

Problem

Packages with weights weights sit on a conveyor belt and must be shipped in the given order. Each day you load the ship with packages from the front of the belt until adding the next one would exceed the ship's capacity. Return the least weight capacity that gets every package shipped within days days.

Example 1

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

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

With capacity 15 in the first example the days are [1..5], [6, 7], [8], [9], [10].

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.