Koko Eating Bananas

MediumBinary Search

Koko has found several piles of bananas: piles[i] is the number of bananas in pile i. The guards come back in h hours.

Koko picks a fixed eating speed k, a positive integer number of bananas per hour. Every hour she chooses one pile and eats k bananas from it. If that pile has fewer than k bananas left, she finishes it and spends the rest of the hour resting: she never starts a second pile within the same hour.

Return the smallest speed k that lets her finish every pile within h hours.

Constraints

  • 1 <= piles.length <= 10^4
  • piles.length <= h <= 10^9 (so a fast enough speed always exists)
  • 1 <= piles[i] <= 10^9

Examples

Example 1

Input: piles = [4,9,2,7], h = 6
Output: 5

At 4 bananas per hour the piles take 1 + 3 + 1 + 2 = 7 hours, one too many. At 5 per hour they take 1 + 2 + 1 + 2 = 6 hours, which fits.

Example 2

Input: piles = [12,5,25,8], h = 4
Output: 25

There are 4 piles and only 4 hours, so each pile must be finished in one hour. The speed must be at least the biggest pile, 25.

Example 3

Input: piles = [10], h = 3
Output: 4

At 3 per hour the pile takes 4 hours (3 + 3 + 3 + 1). At 4 per hour it takes 3 hours (4 + 4 + 2).

JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update

/**
 * @param {number[]} piles
 * @param {number} h
 * @return {number}
 */
function minEatingSpeed(piles, h) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.