二分搜索

class Solution(object):
def shipWithinDays(self, weights, D):
"""
:type weights: List[int]
:type D: int
:rtype: int
"""
l, r, middle = max(weights), sum(weights), 0 # start the binary search strategy
while l < r:
middle = int((l + r) / 2)
# check whether this capacity(middle value) is best
days, w = 1, 0
for i, val in enumerate(weights):
w += val
if w > middle:
days += 1
w = val if days > D:
l = middle + 1
if days <= D:
r = middle return l

Leetcode 1014. Capacity To Ship Packages Within D Days的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 我的第一个Python小程序

    猜年龄,如果大了提示小点,如果小了,提示大点 涉及的知识点: 1.变量 2.注释 3.接收交互式的输入 4.类型转换 5.while循环 6.if..elif..else多条件分支语句 # Autho ...

  2. LVS Nginx Haproxy对比

    一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术. 具体的应用需求还得具体分析,如果是中小型的Web应用,比如日PV小于1000万,用Nginx就完全可以了: 如果机器不少,可以 ...

  3. 编译安装 nginx的http_stub_status_module监控其运行状态

    步骤: 1 编译nginx,加上参数 --with-http_stub_status_module 以我自己的编译选项为例: #配置指令 ./configure --prefix=/usr/local ...

  4. Linux:文件

    Linux:文件 文件属性 用户分为三种:文件拥有者.群组以及其它人,对不同的用户有不同的文件权限. 使用 ls 查看一个文件时,会显示一个文件的信息,例如 drwxr-xr-x. 3 root ro ...

  5. 深度学习1--ubuntu14.04+win10双系统

    相当多的内容参考的百度经验https://jingyan.baidu.com/article/eb9f7b6d79a7b4869364e885.html?qq-pf-to=pcqq.group 还可以 ...

  6. MongoDB环境配置

    在官网上下载MongoDB可执行文件安装在电脑上后,想要运行需先安装路径下新建一个data文件夹,再在里面新建db文件夹用户存放数据库文件和相关配置. 在bin目录里面运行命令行: 这样MongoDB ...

  7. JQuery3 的新变化

    1. for-of 循环 for-in 循环不被推荐遍历数组,forEach 循环不能中断,for-of 循环(ES6)则弥补了前两者的不足,又添加了更多拓展(比如能遍历字符串,DOM 元素等) 因此 ...

  8. 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?

    重定向与请求分派 “局部”参数——ServletConfig——servlet初始化参数 “全局”参数——ServletContext——上下文初始化参数 Web app的“构造器”——Servlet ...

  9. 多校hdu-5775 Bubble sort(线段树)

    题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...

  10. 【P2629】好消息,坏消息(前缀和+单调队列优化DP)

    一激动一颓就会忘了总结... 前面的大黄题就不总结了. 这个题我只想说一声艹,一开始的思路就是正确的,然后计算的时候有了一个瑕疵,不过很快也就改过来了.然后却一直连样例都过不了.仔仔细细看了老半天,经 ...