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. accept()函数用来告诉Qt,事件处理函数“接收”了这个事件,不要再传递;ignore()函数则告诉Qt,事件处理函数“忽略”了这个事件,需要继续传递(看一下QWidget::mousePressEvent的实现,最为典型。如果希望忽略事件,只要调用父类的响应函数即可)

    QEvent的accept()和ignore()一般不会用到,因为不如直接调用QWidget类的事件处理函数直接,而且作用是一样的,见下面的例子. 推荐直接调用QWidget的事件处理函数.而不是调用 ...

  2. Genymotion设置网络桥接

    1,打开Genymotion,找到对应的模拟器,点击“设置”按钮 2,在网络选项中选择桥接 Bridge

  3. MVC Web Api 发布到Azure报错

    I fixed this by reinstalling the NuGet package, which corrects broken dependencies. From the package ...

  4. Android5.1修改以太网MAC地址(SElinux)【转】

    本文转载自:http://blog.csdn.net/LoongEmbedded/article/details/71477203 最近高通平台Android5.1项目中有个关于设置以太网MAC的需求 ...

  5. python dig trace 功能实现——通过Querying name server IP来判定是否为dns tunnel

    dns tunnel确认方法,查询子域名最终的解析地址: 使用方法:python dig_trace.py  "<7cf1e56b 67fc90f8 caaae86e 0787e907 ...

  6. 【FFT初识】

      FFT在用于解决多项式乘法A*B(A和B为多项式,形如a0+a1*x^1+a2*x^2....)的时候,通俗地解释就是: 原理:先根据各自的系数各自转化为对应的向量(O(nlogn)),然后向量相 ...

  7. appium学习【三】:截图时,图片命令中包含当前的函数名,以区分错误是在哪个函数报的

    import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 print sys._getframe().f_code.co ...

  8. bzoj3995

    线段树 额 计蒜客竟然把这个出成noip模拟题... 这个东西很像1018,只不过维护的东西不太一样 然后我参考了fuxey大神的代码,盗一波图 具体有这五种情况,合并请看代码,自己写了一个结果wa了 ...

  9. MySQL之不得不说的keepsync和trysync

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 开宗明义,keepsync和trysync是网易MySQL分支版本InnoSQL的两个参数,非常重要的两个参 ...

  10. 码云 fatal: Authentication failed for

    最近push代码到码云时,push失败,提示fatal: Authentication failed for,解决方法就是: 在git命令行中输入 git config --system --unse ...