Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Koko Eating Bananas

Medium Acceptance 68.00% Points 30.00

Koko loves to eat bananas. There are n piles of bananas, and the ith pile has piles[i] bananas. The guards have gone and will return after h hours. Koko can decide her eating speed of k bananas per hour. Each hour, she chooses one pile and eats k bananas from that pile. If the pile has fewer than k bananas, she eats all of them instead and does not eat any more bananas during that hour. Return the minimum integer k such that she can eat all the bananas within h hours.

Examples
Example 1

Example 1

Input: piles = [3,6,7,11], h = 8

Output: 4

Explanation: If Koko eats 4 bananas per hour: 4 from pile 3 ? 1 hour 4 + 3 from pile 7 ? 2 hours 4 + 2 from pile 6 ? 2 hours 4 + 4 + 3 from pile 11 ? 3 hours Total time = 8 hours, so minimum speed is 4.

Example 2

Example 2

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

Output: 30

Explanation: Koko must eat at speed 30 bananas per hour to finish all piles within 5 hours.

Example 3

Example 3

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

Output: 23

Explanation: At speed 23 bananas per hour, Koko can finish all bananas in exactly 6 hours, which is the minimum possible speed.

Hints
Hint 1
N/A
Constraints
  • 1 = piles.length = 104
  • 1 = piles[i] = 10?
  • 1 = h = 10?
Companies
Apple
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions