leetcode 1011
class Solution {
public:
int shipWithinDays(vector<int>& weights, int D) {
int need_days=weights.size();//初始化需要的天数
int ans_cweight=0;
for(int i:weights){ans_cweight=max(i,ans_cweight);}//承重能力应满足单个最大的重量
while(need_days>D)
{
//cout<<"ans_cweight = "<<ans_cweight<<endl;
int temp=0;
int temp_day=1;
for(int i=0;i<weights.size();++i)
{
//cout<<"weight[i] = "<<weights[i]<<endl;
if(temp+weights[i]<=ans_cweight) temp+=weights[i];
else
{
//cout<<"temp ="<<temp<<endl;
temp_day++;
temp=weights[i];
}
}
need_days=temp_day;
if(need_days>D) ans_cweight++;
//cout<<"need_days = "<<need_days<<endl;
}
return ans_cweight;
}
};
写的超时代码