Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

63. Koko Eating Bananas

MediumDay 18 of 60Binary Search

Koko has piles of bananas, where piles[i] is the size of pile i, and the guards return in h hours. She picks an eating speed k bananas per hour. Each hour she chooses one pile and eats k bananas from it; if the pile has fewer than k, she finishes it and waits for the rest of that hour. Return the smallest integer k that lets her finish all piles within h hours.

Example 1
Input: piles = [3,6,7,11], h = 8
Output: 4
At speed 4 she needs 1 + 2 + 2 + 3 = 8 hours.
Example 2
Input: piles = [30,11,23,4,20], h = 5
Output: 30
Constraints
  • 1 <= piles.length <= 10^4
  • piles.length <= h <= 10^9
  • 1 <= piles[i] <= 10^9
Hints
Frequently asked at: Google, Amazon, Meta, Atlassian
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min