Hone logo
Hone
Problems

Koko Eating Bananas

Koko loves to eat bananas. There are n piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in h hours. Koko can decide her bananas-per-hour eating speed k. Each hour, she chooses a pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during that hour. Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

Your task is to find the minimum integer eating speed k such that Koko can eat all the bananas within h hours.

Problem Description

Koko needs to eat all the bananas from n different piles. The number of bananas in each pile is given by an array piles. Koko can choose an eating speed k (bananas per hour). For each hour, Koko picks one pile and eats up to k bananas from it. If a pile has fewer than k bananas, she eats all of them and doesn't move to another pile in the same hour. She must finish all bananas before the guards return in h hours.

Requirements:

  • Find the smallest possible integer k (eating speed).
  • k must be a positive integer.
  • Koko eats from one pile per hour.
  • If a pile has x bananas and Koko's speed is k, it takes ceil(x / k) hours to finish that pile.

Edge Cases to Consider:

  • What if h is exactly equal to the total number of bananas?
  • What if h is much larger than the total number of bananas?
  • What if there's only one pile of bananas?

Examples

Example 1:

Input: piles = [3, 6, 7, 11], h = 8
Output: 4
Explanation:
If Koko eats 1 banana/hour, she takes 3+6+7+11 = 27 hours. Too slow.
If Koko eats 3 bananas/hour, she takes 1+2+3+4 = 10 hours. Too slow.
If Koko eats 4 bananas/hour, she takes 1+2+2+3 = 8 hours. This is the minimum speed.

Example 2:

Input: piles = [30, 11, 23, 4, 20], h = 5
Output: 30
Explanation:
To eat all piles in 5 hours, Koko must eat at least the speed of the largest pile, which is 30.
Hours per pile: ceil(30/30) + ceil(11/30) + ceil(23/30) + ceil(4/30) + ceil(20/30) = 1 + 1 + 1 + 1 + 1 = 5 hours.

Example 3:

Input: piles = [30, 11, 23, 4, 20], h = 6
Output: 23
Explanation:
If Koko eats at speed 23:
Hours for pile 1 (30): ceil(30/23) = 2
Hours for pile 2 (11): ceil(11/23) = 1
Hours for pile 3 (23): ceil(23/23) = 1
Hours for pile 4 (4): ceil(4/23) = 1
Hours for pile 5 (20): ceil(20/23) = 1
Total hours = 2 + 1 + 1 + 1 + 1 = 6 hours.
This is the minimum speed that allows Koko to finish within 6 hours.

Constraints

  • 1 <= piles.length <= 10^4
  • piles.length <= h <= 10^9
  • 1 <= piles[i] <= 10^9
  • The input piles will be an array of positive integers.
  • The input h will be a positive integer.
  • The solution should aim for an efficient time complexity.

Notes

  • The total time taken to eat all bananas at a speed k can be calculated by summing up the hours needed for each pile. The hours for a single pile p are ceil(p / k).
  • Consider the range of possible values for k. The minimum possible speed is 1. What's the maximum possible speed that would ever be necessary?
  • This problem can be efficiently solved using a binary search approach on the possible values of k.
Loading editor...
plaintext