P9 · 2 seconds · 256 MB

Minimum Eating Speed

You have `n` piles of bananas. Pile `i` has `piles[i]` bananas, and you have `h` hours to finish every pile. Each hour you choose one pile and eat up to `k` bananas from it. If the pile has fewer than `k` bananas, you still spend the whole hour. You may not eat from two piles in the same hour. Return the smallest integer speed `k` such that you finish all piles in at most `h` hours. It is guaranteed that `h ≥ piles.length` (you need at least one hour per pile).

function minSpeed(piles, h) → number

CONSTRAINTS

  • 1 ≤ piles.length ≤ 10⁴
  • piles.length ≤ h ≤ 10⁹
  • 1 ≤ piles[i] ≤ 10⁹
Input: piles = [3, 6, 7, 11], h = 8
Output: 4
Speed 4 uses 1+2+2+3 = 8 hours. Speed 3 uses 10 hours, which is too slow.
Input: piles = [30, 11, 23, 4, 20], h = 6
Output: 23
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.