Koko has piles of bananas and h hours. Each hour she picks a pile and eats k bananas from it (or the whole pile if it has fewer). Return the minimum integer speed k that lets her finish every pile within h hours.
Hours needed only go down as k goes up, so binary-search k in [1, max(piles)] for the smallest speed that fits in h hours.
1function minEatingSpeed(piles: number[], h: number): number {2let lo = 1, hi = Math.max(...piles);3while (lo < hi) {4const k = (lo + hi) >> 1;5const hours = piles.reduce((s, p) => s + Math.ceil(p / k), 0);6if (hours <= h) hi = k;7else lo = k + 1;8}9return lo;10}
Speed is between 1 and the biggest pile, 11.
Space: play/pause · ←/→: step