leetcode_1014. Capacity To Ship Packages Within D Days
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
传送带要在D天内把所有货物传送完,但是传送带每天有传送容量的限制,问保证货物在D天内传送完的最小容量限制。货物重量以weights[]给出。
1 <= D <= weights.length <= 50000
1 <= weights[i] <= 500
一开始想从weights来求最小容量,发现无论如何都不能达到线性时间复杂度。想了好久,突然想到可以二分最小容量,然后对容量验证是否能达到要求。
此外,容量初始最小值为单个货物的最大重量,容量最大值为所有货物重量加和。
class Solution
{
public:
bool can(vector<int>& weights, int D, int maxn)
{
int temp=,i=,day=;
while(i<weights.size())
{
while(i<weights.size() && temp+weights[i]<=maxn)
{
temp+=weights[i];
i++;
}
if(i<weights.size()&&temp+weights[i]>maxn)
{
temp=;
day++;
}
if(i>=weights.size() && temp<=maxn && temp>)
day++;
}
return day<=D;
} int shipWithinDays(vector<int>& weights, int D)
{
int sum_weight=, max_item_weight=INT_MIN;
for(int i=; i<weights.size(); i++)
{
sum_weight+=weights[i];
max_item_weight = max(max_item_weight, weights[i]);
}
int low=max_item_weight, high=sum_weight, res=;
while(low<=high)
{
int mid = (low+high)/;
if(can(weights,D,mid))
{
res=mid;
high = mid-;
}
else
low = mid+;
}
return res;
}
};
leetcode_1014. Capacity To Ship Packages Within D Days的更多相关文章
- Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)
Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...
- [Swift]LeetCode1011. 在 D 天内送达包裹的能力 | Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- Capacity To Ship Packages Within D Days LT1011
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- 128th LeetCode Weekly Contest Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- LeetCode 1011. Capacity To Ship Packages Within D Days
原题链接在这里:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 题目: A conveyor belt h ...
- Leetcode: Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- 【leetcode】1014. Capacity To Ship Packages Within D Days
题目如下: A conveyor belt has packages that must be shipped from one port to another within D days. The ...
- 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Lightoj 1003 - Drunk(拓扑排序)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- BZOJ_4177_Mike的农场_最小割
BZOJ_4177_Mike的农场_最小割 Description Mike有一个农场,这个农场n个牲畜围栏,现在他想在每个牲畜围栏中养一只动物,每只动物可以是牛或羊,并且每个牲畜围栏中的饲养条件都不 ...
- Tomcat 在Mac OS X中的安装和配置
简单介绍: 1.Tomcat是目前比较流行的Web应用服务器,它是一个轻量级的应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. 2.Tomcat运行时所 ...
- SimpliciTI协议栈
SimpliciTI组网过程介绍 1.SimpliciTI支持点对点和星形的网络拓扑结构. 下面介绍以AP为中心的SimpliciTI网路协议的星形拓扑结构通信过程 1)当ED节点上电之后就扫描信 ...
- Jmeter压测Thrift服务接口
此文已由作者夏鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Apache Jmeter是基于Java开发的性能测试工具,支持多种协议的测试,包括:Web(HTTP/HTT ...
- E20180219-hm-xa
comparison n. 比较,对照; [语] 比喻; 比较级; conjunction n. 连接; 连词; 联合,结合物; (恒星.行星等的) 合; [例句] assignment n. 分 ...
- Linux 常用命令十四 killall和pkill
用killall杀死所有同名的进程. wang@wang:~/workpalce/git$ ps -aux | grep vim wang pts/ S+ : : vim a wang pts/ S+ ...
- 【插件开发】—— 14 Site is incorrect!编辑器启动报错!
前言 博文纵览 最近在弄编辑器的时候出现了一个十分尴尬的错误!这里收录一下: BUG如下图所示: 目测堆栈,与自己开发的代码无关.完全是Eclipse自己初始化的时候遇到了问题,最头疼的就是这种问题. ...
- 微信小程序中如何使用setData --- 修改数组对象、修改对象
看代码吧~ 这是修改对象 this.setData({ allStageIndex: e.detail.value, [`projectDetailsData.stage`]: this.data.a ...
- 7步教你使用git命令上传本地代码至github仓库(小白向)
前言 1:首先你需要知道github和git分别是什么?(众所周知github是全球最大同性交友网站233333) github:国外一个免费托管代码的开源网站,每位注册用户都拥有专属的个人仓库(个人 ...