18 · Binary Search

#875 Koko Eating Bananas

Mediumsolve on LeetCode ↗

frequently reported atAmazonAdobeMicrosoftBloombergMetaGoogle

the problem

In plain words

Koko eats k bananas per hour from one pile; if a pile has fewer than k she finishes it and waits for the next hour. Given piles and h hours, return the minimum integer k that lets her finish in time.

in piles = [3,6,7,11], h = 8

out 4

At 4/hour: 1 + 2 + 2 + 3 = 8 hours.

in piles = [30,11,23,4,20], h = 5

out 30

in piles = [30,11,23,4,20], h = 6

out 23

  • 1 ≤ piles.length ≤ 10⁴
  • piles.length ≤ h ≤ 10⁹
  • 1 ≤ piles[i] ≤ 10⁹

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. #1011
    2. #69
      Sqrt(x)
      Easyin Big-O
    3. #1631
      Path With Minimum Effort
      Mediumin Shortest Paths