当前位置:网站首页>1011. Ability to deliver packages within D days
1011. Ability to deliver packages within D days
2022-07-21 17:02:00 【baixiaofei567】
Good question , At first I thought dfs Explosive search , Or I read the solution of sister Sanye directly .
It's a binary search problem , What we are looking for is to be able to D The minimum carrying capacity delivered within days . Without considering the number of days, the minimum load is enough to transport the largest goods , The maximum carrying capacity is that all goods are transported in one day .
The left border is max, There are boundaries sum, Split it in two , Write a check function , This is very important , Judge the current transportation capacity , This is our standard to narrow the search scope .check Internally, it is calculated that the current transportation capacity will take several days to complete
If the delivery days <=D,mid Can't throw , Continue to look on the left , See if you can find something smaller . If >D, It is necessary to increase the load , Go to the right , and mid No more .
Last returned r, Because we control <= When mid Do not discard into r, When it's over ,r Is the smallest solution
class Solution {
public:
bool check(vector<int>& weights,int t,int D){
int day = 0;
for(int i = 0; i < weights.size();){
day++;
int sum = 0;
// Without crossing the boundary and exceeding the load , It can deliver
while(i < weights.size() && sum + weights[i] <= t){
sum += weights[i++];
}
}
return day <= D;
}
int shipWithinDays(vector<int>& weights, int D) {
// Use two points to find the transportation between the minimum transportation capacity and the maximum transportation capacity D Days of
// If the current transportation capacity exceeds D Days go to the right , Less than or equal to the left . Why is it less than or equal to
// Finally back to r, Why?
int l = 0, r = 0;
for(int x : weights){
l = max(l,x);
r += x;
}
while(l < r){
int mid = l + (r-l)/2;
if(check(weights,mid,D)){
r = mid;
}
else{
l = mid + 1;
}
}
// Because we control <= When mid Do not discard into r, When it's over ,r Is the smallest solution
return r;
}
};
边栏推荐
猜你喜欢
随机推荐
BCG属性列表
有哪些高质量的自学网站?
让我们用ArcGIS制作一张好看的中国月度气温图
ORA-01795 列表中的最大表达式数为1000
[guaranteed research] - sort out and share the problems existing in the previous school reexamination interview
Scala exercises student scores case
北森招股书:赛道优势凸显,一体化+中大客户是加分项
中国邮储银行 IT 集采:服务器、阵列、交换机、路由器、防火墙等
Pit encountered by pl/sql
Selected papers of "deep neural network machine learning column"
C#中Internal关键字的总结
這份 pip 使用小抄,要有全有多全!
三丰智能唐海芹:为「三农」插上智慧翅膀,做智慧农业「引领者」| 镁客·请讲
经典自动化面试题
16个你要注意的SQL注入测试点
938. 二叉搜索树的范围和
消息队列-rockerMQ开发实战
Reinforcement learning weekly 54: i-sim2real, griddlyjs & the latest application of inverse reinforcement learning (IRL)
Pedestrian recognition Reid
项目经理如何做好“老板”项目