Binary search over the answer space to find the slowest eating rate that finishes in time.
About this algorithm
Koko has piles of bananas and h hours before the guards return. Each hour she picks one pile and eats up to k bananas from it; if the pile has fewer, she finishes it and waits for the next hour. Find the smallest whole number k that lets her finish every pile in time. The answer is found by binary searching over possible speeds.
This is the standard example of binary search on the answer: there is no array to search, but there is a range of possible answers and a quick yes or no test for each one, and the test flips only once (too slow below some speed, fast enough at and above it). The same shape solves shipping packages within D days, splitting an array into parts with the smallest largest sum, and the gas station page, which does it with real numbers instead of whole ones. Plain binary search on a sorted array is the page to read first if halving is new.
The slowest useful speed is 1 banana an hour. The fastest you ever need is the largest pile, since at that speed every pile takes exactly one hour. So lo = 1 and hi = max(piles).
Take mid = floor((lo + hi) / 2). Eating at mid, a pile of p bananas takes ceil(p / mid) hours. Add those up over every pile.
If the total is h hours or less, mid works. Remember it as the best answer so far and look for something slower: hi = mid - 1.
If the total is more than h, every speed up to mid is too slow as well: lo = mid + 1. Stop when lo passes hi; the best speed remembered is the answer.
The page loads piles [3, 6, 7, 11] and h = 8, so speeds run from 1 to 11. Try 6: 1 + 1 + 2 + 2 = 6 hours, fast enough, so hi = 5. Try 3: 1 + 2 + 3 + 4 = 10 hours, too slow, so lo = 4. Try 4: 1 + 2 + 2 + 3 = 8 hours, exactly on time, so 4 is the best so far and hi = 3. Now lo is above hi and the search ends: Koko needs 4 bananas an hour. Three guesses settle it, where trying every speed from 1 upward would have taken four.
ceil(p / k) can be computed with whole numbers as (p + k - 1) / k using integer division. It avoids floating point rounding and is what most solutions use.
Time: each guess sums over all n piles, and halving the speed range 1 to m (the largest pile) takes about log2(m) guesses. Space: lo, hi, mid and a running hour count are all that is kept.