二分搜索

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 4 函数的参数,内置函数,装饰器,生成器,迭代器,

    一.函数的参数: 1.位置参数:调用函数时根据函数定义的参数位置来传递参数. 2.关键字参数:用于函数调用,通过“键-值”形式加以指定.可以让函数更加清晰.容易使用,同时也清除了参数的顺序需求. 3. ...

  2. MVC,MVP和MVVM区别

    复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身很容易理解,但是要讲清楚,它与衍生的 MVP ...

  3. 大型网站系统与 Java 中间件实践

    http://wanglizhi.github.io/2016/07/27/JavaWeb-And-MiddleWare/ 第一章 分布式系统介绍 分布式系统的定义:组件分布在网络计算机上,组件间仅仅 ...

  4. linux alsa pcm(此pcm非硬件pcm接口)

    转:https://blog.csdn.net/crycheng/article/details/7095899 CODEC :音频芯片的控制,比如静音.打开(关闭)ADC(DAC).设置ADC(DA ...

  5. 如何理解Java程序使用Unicode字符集编写

    Java采用UTF-16编码作为内码,也就是说在JVM内部,文本是用16位码元序列表示的,常用的文本就是字符(char)和字符串(String)字面常量的内容.注:UTF-16是Unicode字符集的 ...

  6. 0802 DRF 视图

    昨日回顾: 1. Serializer(序列化) 1. ORM对应的query_set和ORM对象转换成JSON格式的数据 1. 在序列化类中定义自定义的字段:SerializerMethodFiel ...

  7. linux上安装程序出现的问题汇总

    1.程序在编译过程中出现:variable set but not used [-Werror=unused-but-set-variable] 解决方法:将configure文件和Makefile文 ...

  8. iOS_KVC与KVO

    目 录: 一.KVC   二.KVO                                                                                   ...

  9. OC_内存管理

      引言:      1.OC中的对象都是分配在堆中的                声明对象的格式:                     Person *person = [Person new ...

  10. 斯坦福机器学习视频笔记 Week7 支持向量机 Support Vector Machines

    SVM被许多人认为是最强大的“黑箱”学习算法,并通过提出一个巧妙选择的优化目标,今天最广泛使用的学习算法之一. Optimization Objective 根据Logistic Regression ...