原题链接:Leecode 1011. 在 D 天内送达包裹的能力
class Solution {
public:
int shipWithinDays(vector<int>& weights, int days) {
auto m=max_element(weights.begin(),weights.end());
int l=*m,r=accumulate(weights.begin(),weights.end(),0);
while(l<r)
{
int mid=l+(r-l)/2;
int d=0,c=0;
for(auto w:weights)
{
if(c+w>mid) { d++;c=0; }
c+=w;
}
d= c==0 ? d:d+1;
if(d>days) l=mid+1;
else r=mid;
}
return l;
}
};