原题题目
代码实现(首刷自解)
class Solution {
public:
bool can_arrive(vector<int>& dist,double hour,int speed)
{
if(!speed) return false;
double arrive_hours = 0;
for(const auto& num:dist)
{
arrive_hours += (double)num/speed;
if(arrive_hours > hour) return false;
arrive_hours = ceil(arrive_hours);
}
return true;
}
int minSpeedOnTime(vector<int>& dist, double hour) {
int left = 0,right = pow(10,7);
while(left < right)
{
int mid = (left + right)/2;
if(can_arrive(dist,hour,mid)) right = mid;
else left = mid+1;
}
return can_arrive(dist,hour,left) ? left : -1;
}
};
|