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 <= 500001 <= 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 ...
随机推荐
- wpa_supplicant - 强有力的终端 wifi 配置工具【转】
本文转载自:http://rickgray.me/2015/08/03/useful-command-tool-for-wifi-connection.html 最近网购了一套Raspberry-Pi ...
- Apache POI组件操作Excel,制作报表(四)
Apache POI组件操作Excel,制作报表(四) 博客分类: 探索实践 ExcelApacheSpringMVCServlet 上一篇我们介绍了如何制作复杂报表的分析和设计,本篇结合S ...
- next数组求最小循环节
1.kmp产生的next数组: 最小循环节(长度)=len-next[len]; 证明: ----------------------- ----------------------- k m ...
- 配置JDK环境变量配置及path和classpath的作用
1.环境变量配置 用鼠标右击“我的电脑”->属性->高级->环境变量 JAVA_HOME :D:\Program Files\Java\jdk1.6.0_12(JDK安装路径) Pa ...
- js页面报错javax.servlet.jsp.PageContext cannot be resolved to a type解决
构建了一个maven项目但是项目创建好的jsp总会报错javax.servlet.jsp.PageContext cannot be resolved to a type,但是不影响项目运行.但总归难 ...
- Spring事物注意事项
一.尽量用注解声明事务 过去开发喜欢用tx:advice+aop命名空间方式来配置事务,一次配置对满足切点规则的方法永久生效.但也可能因此导致事务滥用,在不需要用到事务的地方用了会影响系统的并发性能. ...
- bzoj1566 [NOI2009]管道取珠——DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1566 一眼看上去很懵... 但是答案可以转化成有两个人在同时取珠子,他们取出来一样的方案数: ...
- js追加子元素
在页面加载完毕后,向div元素追加span子元素 <html><head><title>js</title><script type=" ...
- 关于centOS7的一些笔记
使用systemctl查看 开启 关闭服务: 查看: systemctl status arcgis.server 开启: systemctl start arcgis.server 关闭: syst ...
- jquery 3D云
http://www.jq22.com/jquery-info1325 http://demo.jq22.com/jquery-cloud-141217202931 下载地址: http://www. ...