875. 爱吃香蕉的珂珂
作者:互联网
/**
-
@author: 施龙青
-
@description 875. 爱吃香蕉的珂珂
-
@date: 2022/6/7 1:24
*/
public class Main001 {
//测试
public static void main(String[] args) {
Main001 main001 = new Main001();
int[] a = new int[]{30, 11, 23, 4, 20};
System.out.println(main001.minEatingSpeed(a, 5));
}public int minEatingSpeed(int[] piles, int h) {
int len = piles.length;//二分的是速度,从1开始,到最大香蕉数 int left = 1; int right = -1; for (int pile : piles) { if (right < pile) { right = pile; } } while (left < right) { int mid = (right - left) / 2 + left; if (calculateSum(piles, mid) > h) { left = mid + 1; } else { right = mid; } } return left;
}
int calculateSum(int[] piles, int speed) {
int sum = 0;for (int pile : piles) { //向上取整 sum += (pile + speed - 1) / speed; } return sum;
}
}
标签:香蕉,right,piles,int,875,mid,pile,left 来源: https://www.cnblogs.com/weixing-a2053699418/p/16351357.html