import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[] piles = {30,11,23,4,20};
System.out.println(solution.minEatingSpeed(piles, 6));
}
public int minEatingSpeed(int[] piles, int h) {
int maxValue = Arrays.stream(piles).max().getAsInt();
if (piles.length == h){
return maxValue;
}
int left = 1, right = maxValue, k = maxValue;
while (left < right){
int speed = (left + right) >> 1;
int time = getTime(piles, speed);
// 证明这个速度可以吃完,看看能不能找到速度更块的
if (time <= h){
k = speed;
right = speed;
} else {
// 这个速度吃不完,咋办?吃的速度加一
left = speed + 1;
}
}
return k;
}
private int getTime(int[] piles, int speed){
int countTime = 0;
for (int pile : piles) {
int count = pile % speed == 0 ? pile / speed : (pile / speed )+ 1;
countTime += count;
}
return countTime;
}
}