二分搜索

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. JDBC注册驱动程序的三种方式

    1. Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动 Class.forName("com.mysql.jdbc.Dri ...

  2. print函数end参数的作用

    print函数默认会在末尾添加一个换行符(‘\n’) 加入end=''参数后,不会在末尾添加换行符,而是在末尾添加一个空字符串,end等于什么就会在末尾添加什么 这个只在python3中有效

  3. 深度学习2--安装opencv3.1

    1\opencv的安装参考视频 2\ 以下内容来自:http://blog.csdn.net/l18930738887/article/details/54696148 本人因为被坑过,所以建议各位最 ...

  4. css的继承性理解

    1) 所有的text 相关属性都被继承: 如 font-family font-size; font-style;font-weight;font;font-variant;letter-spacin ...

  5. RHEL(或CentOS)中关于逻辑卷( Logical Volume Manager,LVM)的一些概念及使用LVM的例子

    1.逻辑卷(logical volumes,LV) 卷管理在物理存储之上的抽象层,它使你能够创建逻辑存储卷.和直接使用物理存储相比,这从很多方面提供了更大的灵活性.比如,使用逻辑卷,你将不再受物理磁盘 ...

  6. 【Topcoder】SRM157 DIV2总结

    250分题:简单的二分,就是平常玩的猜数字游戏 代码:GitHub 500分题:给出一个员工一天的打卡时间段,要求求出员工这一天的工资.其中正常上班时间是6:00:00到18:00:00,薪水是wag ...

  7. OpenSSL for Android

    http://blog.csdn.net/xiongmc/article/details/25736041 OpenSSL1)开源项目Guardian Project试图让Android手机也拥有类似 ...

  8. Listening Carefully SP1403S

    Listening Carefully仔细聆听When people talk, listen completely. Most people never listen. ―Ernest Heming ...

  9. IMP导入小记

    1.创建表空间 create tablespace example_tablespace datafile 'e:\****.dbf' size 10m reuse autoextend on nex ...

  10. ACM训练小结-2018年6月23日

    今天题目情况如下:    D题:SG函数相关.    相关知识忘光...已复习.    E题:丧心病狂的模拟题目.    F题:树分块+容斥.    想到了树上莫队,但是糟糕的是不会O(1)/O(lo ...