875. 爱吃香蕉的珂珂
题目链接:875. 爱吃香蕉的珂珂
代码如下:
class Solution
{
public:
int minEatingSpeed(vector<int>& piles, int h)
{
//左边界是1,右边界是所有香蕉的总和
int low=1,high=0;
for(int i=0;i<piles.size();i++) {high=max(high,piles[i]);}
int k=high;
while(low<high)
{
int speed=low+((high-low)>>2);
long time=getTime(piles,speed);
if(time<=h)
{
k=speed;
high=speed;
}
else {low=speed+1;}
}
return k;
}
//计算以mid为速度吃完香蕉的最小时间
long getTime(const vector<int>& piles,int speed)
{
long time=0;
for(int i=0;i<piles.size();i++)
{
int curTime=(piles[i]+speed-1)/speed;
time+=curTime;
}
return time;
}
};