题目如下:

A conveyor belt has packages that must be shipped from one port to another within D days.

The i-th package on the conveyor belt has a weight of weights[i].  Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.

Example 1:

Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
Output: 15
Explanation:
A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10 Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.

Example 2:

Input: weights = [3,2,2,4,1,4], D = 3
Output: 6
Explanation:
A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4

Example 3:

Input: weights = [1,2,3,1,1], D = 4
Output: 3
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1

Note:

  1. 1 <= D <= weights.length <= 50000
  2. 1 <= weights[i] <= 500

解题思路:首先可以确认Capacity的最小值是1,最大值是50000*500。因为知道上下限,所以这里可以采用二分查找的方法。记w[i]为前i个包裹的重量和,显然w是一个递增的数组,假设Capacity为mid,那第一天可以装载的包裹的重量就是在w中最大的小于或者等于mid的元素,这里记为w[j],那第二天能装载的最大重量就是w[j] + mid,同理可以在w中最大的小于或者等于w[j]+mid的元素,因为w是有序的,所以这里继续使用二分查找,如果D天装载的包裹重量能大于或者等于w[-1],那么表示mid满足条件,下一步让 = mid - 1;如果不满足说明则令mid = low + 1,直到找出最小的mid为止。

代码如下:

class Solution(object):
def shipWithinDays(self, weights, D):
"""
:type weights: List[int]
:type D: int
:rtype: int
"""
w = []
low = 0
for i in weights:
low = max(low,i)
if len(w) == 0:
w += [i]
else:
w += [w[-1] + i]
#print w
high = 50000*500
res = float('inf')
import bisect
while low <= high:
mid = (low+high)/2
start = 0
times = 1
tmp_weight = mid
while times <= D:
inx = bisect.bisect_left(w,tmp_weight,start)
if inx == len(w):
break
elif tmp_weight == w[inx]:
if inx == len(w) - 1:
break
tmp_weight = w[inx] + mid
times += 1
else:
tmp_weight = w[inx-1] + mid
times += 1
#print mid,times
if times <= D and tmp_weight >= w[-1]:
res = min(res,mid)
high = mid -1
else:
low = mid + 1 return res

【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

    二分搜索 class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: ...

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

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

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

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. [CSP-S模拟测试]:Graph(图论+贪心)

    题目描述 给定一张$n$个点$m$条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通你想在这张图上进行若干次旅游,每次旅游可以任选一个点$x$作为起点,再走到一个与 $x$直接有边相连的点$ ...

  2. Drone 的插件 - Docker 插件

    Drone 插件市场 Drone 插件文档 原文地址 - Docker 插件的手册 Docker 插件可以用于构建镜像及发布镜像到 Docker registry.下面的 pipeline 配置,就使 ...

  3. WebService登陆验证四种方式

    在这个WEB API横行的时代,讲WEB Service技术却实显得有些过时了,过时的技术并不代表无用武之地,有些地方也还是可以继续用他的,我之所以会讲解WEB Service,源于我最近面试时被问到 ...

  4. 公司C++规范学习

    目录 公司C++规范学习 语法部分 风格/约定 公司C++规范学习 语法部分 class和struct关键字的选择:class表示被封装的用户自定义类型,不公开定义非静态数据成员,struct表示数据 ...

  5. docker 一小时快速入门之利用docker安装Redis

    利用docker方式快捷安装redis 该方式默认下载的最新版本镜像,如需要下载指定版本在redis后面跟:版本号 docker pull redis 查看当前下载redis的镜像 docker im ...

  6. leetcode.字符串.344反转字符串-Java

    1. 具体题目 编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出.不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一 ...

  7. java_第一年_JavaWeb(7)

    JSP执行过程 客户端发出请求访问JSP文件 JSP Container将要访问的JSP文件转译为Servlet的源代码(转译时期),并将其编译成.class文件(编译时期): 执行编译后的.clas ...

  8. P3826 [NOI2017]蔬菜

    传送门 注意每一单位蔬菜的变质时间是固定的,不随销售发生变化 固定的...... 就是每一个单位的蔬菜在哪一天变质是早就定好了的 发现从第一天推到最后一天很不好搞 考虑反过来,从最后一天推到第一天,这 ...

  9. Linux就该这么学10学习笔记

    参考链接:https://www.linuxprobe.com/chapter-10.html 网站服务程序 第1步:把光盘设备中的系统镜像挂载到/media/cdrom目录. [root@linux ...

  10. 二、TortoiseSVN 合并、打分支、合并分支、切换分支

    一.合并 点击Edit conflict来编辑冲突: 在合并后的枝干对应栏中编辑后,Save保存后关闭. 二.TortoiseSVN 打分支.合并分支.切换分支 1.SVN打分支 方式一:先检出,再打 ...