
Leetcode Problem 875: Koko Eating Bananas
There are n piles of bananas, and the guards will return in h hours. Each hour, Koko picks one pile and eats up to k bananas from it — if the pile has fewer than k bananas, she eats all of them and waits for the next hour. Find the minimum integer eating speed k such that Koko can finish all piles before the guards return.
Constraints:
h (available hours) is at least as large as the number of piles.h can be at most 1,000,000,000.function hoursFor(piles: number[], k: number) {
let hoursNeeded = 0;
for (const pile of piles) {
hoursNeeded += Math.ceil(pile / k);
}
return hoursNeeded
}
function minEatingSpeed(piles: number[], h: number): number {
let left = 1
let right = Math.max(...piles)
while (left < right) {
const mid = left + Math.floor((right - left) / 2)
if (hoursFor(piles, mid) > h) {
left = mid + 1
} else {
right = mid
}
}
return left
}
console.log(minEatingSpeed([30,11,23,4,20], 5)); // Output: 30