875. 爱吃香蕉的珂珂
小于 1 分钟
875. 爱吃香蕉的珂珂中等
解题思路:
定义 left = 1,right = Integer.MAX_VALUE
使用二分查找,计算时间 sum
sum <= h
,res = Math.min(mid,res);right = mid - 1;
sum > h
,left = mid + 1;
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int res = Integer.MAX_VALUE;
int left = 1;
int right = Integer.MAX_VALUE;
while (left <= right){
long sum = 0;
int mid = left + (right - left) / 2;
for (int pile : piles) {
sum += pile % mid == 0 ? pile / mid : pile / mid + 1;
}
if(sum <= h){res = Math.min(mid,res);right = mid - 1;}
else {left = mid + 1;}
}
return res;
}
}
Powered by Waline v2.15.5