https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/

传送带要在D天内把所有货物传送完,但是传送带每天有传送容量的限制,问保证货物在D天内传送完的最小容量限制。货物重量以weights[]给出。

  1. 1 <= D <= weights.length <= 50000
  2. 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的更多相关文章

  1. Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)

    Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. LeetCode 1011. Capacity To Ship Packages Within D Days

    原题链接在这里:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 题目: A conveyor belt h ...

  6. 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 ...

  7. 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 ...

  8. 【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  ...

  9. 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 一步一步学Silverlight 2系列(3):界面布局

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  2. ios app 上架AppStore

    一.证书的导出      1.1 前期工作        首先你需要有一个苹果的开发者帐号,一个Mac系统.        如果没有帐号可以在打开http://developer.apple.com/ ...

  3. VS2013文件同步插件开发

    一.插件功能描述 插件监控一个xml文件,当该文档有添加新元素在保存的时候将新增的元素同步到指定的目录下. 二.模板的选择 由于该功能是跟代码编辑有关的,要监控文档的保存事件,所以要在文档打开的时候就 ...

  4. 一个tomcat部署多个应用实例总结

    项目组有好几个项目需要运行,之前项目少,一个tomcat对应一个项目还能应付,但现在项目多了,要是再一个tomcat对应一个项目的话,一方面看起来很业余,一方面也加大服务器的维护难度.所以现在需要对t ...

  5. AutoMapper封装类

    /// <summary> /// AutoMapper扩展帮助类 /// </summary> public static class AutoMapperHelper { ...

  6. JS 对java返回的json格式的数据处理

    var dataObj=eval("("+res+")"); alert(dataObj.billBuy) //res是如下的数据 {"billBuy ...

  7. Visual Studio一些插件

    让JavaScript像C#一样支持Region http://www.cnblogs.com/codealone/p/3647127.html

  8. 比Android更深远的改变世界——谷歌开源人工智能系统TensorFlow文档中文版

    OpenStack中国社区编者按:开源无处不在,特别在基础创新领域,未来系统软件都会是开源为主流:2015年11月9日,Google于开源了其第二代人工智能系统Tensorflow,如同6年前同样开源 ...

  9. E20180309-hm-xa

    conformance   n. 顺应,一致; symmetric   adj. 相称性的,均衡的; raw  adj. 生的,未加工的; 无经验的; 新近完成的; 发炎的,疼痛的; exceed  ...

  10. UOJ #206. 【APIO2016】Gap【交互题】

    参考:https://blog.csdn.net/clover_hxy/article/details/70767653 人生第一次交互题...不是很难但是思维和传统题差别挺大的(以及并不会本地测试= ...