Skip to contentKEEP THE CRAFTTHE SANCTUARY

Koko Eating Bananas

Binary search the eating speed: find the slowest speed that still finishes every pile in time.

The problem

Implement Solution.minEatingSpeed(piles, h). Koko eats bananas at a whole-number speed of k bananas per hour. Each hour she picks one pile and eats k bananas from it; if the pile has fewer than k left, she finishes it and waits for the rest of that hour. Return the smallest k that lets her finish every pile within h hours. For example, piles = [3, 6, 7, 11] and h = 8 returns 4. len(piles) <= h, 1 <= len(piles) <= 10000, 1 <= piles[i] <= 1000000000 and h <= 1000000000.

Starter code

class Solution:
    def minEatingSpeed(self, piles, h):
        pass

Public examples

  1. piles = [3, 6, 7, 11], h = 8gives4
  2. piles = [9], h = 2gives5
  3. piles = [2, 3], h = 5gives1

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.