leetcode 1011. 在 D 天内送达包裹的能力
作者:互联网
二分法
class Solution {
public:
int shipWithinDays(vector<int>& weights, int D) {
int left = 0,right = 25000000;
int n = weights.size();
while(left<right) {
int ship = (left+right)/2;
int tmp = 0;
int day = 1;
for(int i=0;i<n;i++) {
if(weights[i]>ship) {
day=D+1;
break;
}
if(weights[i]+tmp>ship) {
tmp = weights[i];
day+=1;
}else tmp+=weights[i];
}
if(day<=D) {
right = ship;
}else{
left = ship+1;
}
//cout<<left<<" "<<right<<endl;
}
return left;
}
};
标签:tmp,int,1011,weights,ship,left,leetcode,day,送达 来源: https://blog.csdn.net/qq_36704378/article/details/111481195