Find the minimum integer eating speed that lets Koko finish all banana piles within hours.
Problem
Koko has several piles of bananas. In each hour, she chooses one pile and eats bananas from that pile at a fixed integer speed bananas per hour.
If a pile has fewer than bananas, she eats the whole pile in that hour. Koko cannot split an hour across multiple piles.
Given the array of pile sizes and a limit of hours, determine the minimum integer speed such that Koko can finish all bananas within hours.
Goal
Return the smallest positive integer speed that makes the total time required less than or equal to .
Input Format
- An integer array
piles, wherepiles[i]is the number of bananas in the -th pile. - An integer
h, the maximum number of hours available.
Output Format
- Return a single integer: the minimum eating speed
k.
Constraints
1 <= piles.length1 <= piles[i]1 <= h- The answer is guaranteed to fit in a 32-bit signed integer.
Notes
- For a given speed
k, the required hours for one pile is . - The total time is the sum over all piles.
Example 1
Input
piles = [3, 6, 7, 11], h = 8
Output
4
Explanation
At speed 4, the time needed is . No smaller speed can finish in 8 hours.
Example 2
Input
piles = [30, 11, 23, 4, 20], h = 5
Output
30
Explanation
Koko must finish one pile per hour, so she needs a speed large enough to eat the largest pile in one hour. The minimum such speed is 30.
Show 1 more example
Example 3
Input
piles = [30, 11, 23, 4, 20], h = 6
Output
23
Explanation
At speed 23, the hours are . Any smaller speed requires more than 6 hours.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.