原题链接:875. 爱吃香蕉的珂珂
solution:
对结果采用二分,找到满足条件的左端点。
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int l = 1,r = 1e9;
while(l < r) {
int mid = l + r>> 1;
if(EatCheck(piles, mid, h)) r = mid;
else l = mid + 1;
}
return l;
}
//判断能否在以v/h的速度吃完香蕉
//piles:香蕉堆
//v:吃香蕉的速度
//h:警卫离开的时间
bool EatCheck(vector<int> &piles, int v,int h) {
int cnt = 0;
for(auto &pile : piles) {
cnt += pile / v;
if(pile % v != 0) cnt++;
}
return cnt <= h;
}
};