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. 风云流水 jQuery技巧总结 (转)

    jQuery技巧总结 (转) 一.简介 1.1.概述 随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery.m ...

  2. codeforces 686A A. Free Ice Cream(水题)

    题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...

  3. ModelAndView存取数Model、Map、ModelMap

    1.从名字上看ModelAndView中的Model代表模型,View代表视图,这个名字就很好地解释了该类的作用.(对应mvc配置,寻找模型,返回视图.) 2.业务处理器调用模型层处理完用户请求后,把 ...

  4. 腾讯QQ空间应用宽屏接入

    QQ 空间接入宽屏. (与腾讯微博分属两个不同平台) 相关文档:  流动应用画布说明 前端页面规范 多区多服场景说明 应用宽屏根据游戏分为两种.  1: 普通游戏,但想要实现宽屏显示. 2: 多区多服 ...

  5. [知识积累]python3使用xlwt时写入文档字体颜色和边框样式

    可借鉴的网址:https://www.programcreek.com/python/example/39979/xlwt.Alignment 可以直接通过pip安装xlwt 个人理解: xlwt中对 ...

  6. J201700525-hm

    スケルトン 骨組み(ほねぐみ) 骨架 リソース    资源

  7. JavaScript中this的使用方法总结

    JavaScript中this的使用方法总结 在JavaScript中,this的使用分为四种场景,具体请参考阮一峰老师关于this的讲解 第一种情况是纯函数使用 var x =1 ; functio ...

  8. js判断是否为ie浏览器,精确显示各个ie版本

    function IETester(userAgent){     var UA =  userAgent || navigator.userAgent;     if(/msie/i.test(UA ...

  9. 【SpringCloud构建微服务系列】微服务网关Zuul

    一.为什么要用微服务网关 在微服务架构中,一般不同的微服务有不同的网络地址,而外部客户端(如手机APP)可能需要调用多个接口才能完成一次业务需求.例如一个电影购票的手机APP,可能会调用多个微服务的接 ...

  10. ls -l 详解

    ls -l 是文件系统的一个命令,用来查询当前路径的文件的属性.大小等详细信息