Description

Koko loves bananas. There are n piles of bananas, the ith pile has piles[i] bananas. Guards will return in h hours. Koko can eat at most k bananas per hour. If a pile has less than k bananas, she finishes that pile and won't eat more in that hour. Return the minimum integer k such that she can eat all bananas within h hours.

Examples

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

At eating speed 4: pile of 3 takes 1 hour, pile of 6 takes 2 hours, pile of 7 takes 2 hours, pile of 11 takes 3 hours, totaling 8 hours which equals h=8. Speed 4 is the minimum that finishes within 8 hours.

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

She must finish each pile in 1 hour, so k = max(piles) = 30.

Input:piles = [2,2,2,2], h = 6
Output:2
Explanation:

With k=2, Koko can eat each pile in exactly 1 hour: ceil(2/2) = 1 hour per pile. Total time = 1+1+1+1 = 4 hours, which is ≤ 6 hours. With k=1, she would need ceil(2/1) = 2 hours per pile, totaling 8 hours > 6. Therefore, minimum k = 2.

Constraints

  • 1 ≤ piles.length ≤ 10⁴
  • piles.length ≤ h ≤ 10⁹
  • 1 ≤ piles[i] ≤ 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!