binary-search
arrays

Koko loves bananas. There are piles.length piles, and the i-th pile has piles[i] bananas. The guards have gone and will come back in h hours.

Koko decides her eating speed k (bananas per hour). Each hour she picks a pile and eats k bananas from it. If the pile has fewer than k bananas, she eats all of them and will not eat any more during that hour.

Return the minimum integer speed k such that she can finish every pile within h hours.

Because she can only eat from one pile per hour, a pile of p bananas always takes ceil(p / k) hours at speed k.

Input / output

  • Input: piles: int[], h: int
  • Output: int — the minimum feasible speed

Constraints

  • 1 <= piles.length <= 10000
  • piles.length <= h <= 1000000000
  • 1 <= piles[i] <= 1000000000

Follow-up

The hours-needed function is monotonic in k (faster is never slower to finish). How does that monotonicity let you binary-search the answer instead of trying every speed?

Examples

Example 1

Input: piles = [3,6,7,11], h = 8
Output: 4

Example 2

Input: piles = [30,11,23,4,20], h = 5
Output: 30

Example 3

Input: piles = [30,11,23,4,20], h = 6
Output: 23