18 · Binary Search

#1011 Capacity To Ship Packages Within D Days

Mediumsolve on LeetCode ↗

frequently reported atAmazonMetaGoogle

the problem

In plain words

Packages must ship in the given order. Each day the ship is loaded with consecutive packages up to its capacity. Return the least capacity that ships everything within days days.

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

out 15

[1..5] [6,7] [8] [9] [10].

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

out 6

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

out 3

  • 1 ≤ days ≤ weights.length ≤ 5·10⁴
  • 1 ≤ weights[i] ≤ 500

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #875
      Koko Eating Bananas
      Mediumin Binary Search
    2. #778
      Swim in Rising Water
      Hardin Shortest Paths