Koko has n piles of bananas. The guards are gone for h hours. Each hour, Koko picks a pile and eats up to k bananas from it. If the pile has fewer than k, she eats them all and stops eating until the next hour.
Return the minimum integer k such that she can eat all bananas within h hours.
This problem uses binary search on the answer space: search for the smallest eating rate that is feasible.
1 ≤ piles.length ≤ 10⁴piles.length ≤ h ≤ 10⁹1 ≤ piles[i] ≤ 10⁹The answer is in the range [1, max(piles)].
Pick a speed k and you can work out at once whether it's fast enough. A pile of p bananas takes ceil(p / k) hours, since the last hour on a pile can be a partial one, and Koko never moves on to a second pile in the same hour. Add that up over all the piles and compare it with h.
Time O(n log m)Space O(1)
The full walkthrough and code are part of Premium
4