【leetcode】1014. Capacity To Ship Packages Within D Days
题目如下:
A conveyor belt has packages that must be shipped from one port to another within
Ddays.The
i-th package on the conveyor belt has a weight ofweights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given byweights). 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
Ddays.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, 4Example 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, 1Note:
1 <= D <= weights.length <= 500001 <= 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的更多相关文章
- 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 1014. Capacity To Ship Packages Within D Days
二分搜索 class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: ...
- 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 ...
- Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)
Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 170817关于JSON知识点
1. JSON [1] JSON简介 JSON全称 JavaScript Object Notation ...
- [CSP-S模拟测试]:Permutation(线段树+拓扑排序+贪心)
题目描述 你有一个长度为$n$的排列$P$与一个正整数$K$你可以进行如下操作若干次使得排列的字典序尽量小对于两个满足$|i−j|\geqslant K$且$|P_i−P_j|=1$的下标$i$与$j ...
- [CSP-S模拟测试]:回文(hash+二维前缀和)
题目描述 闲着无聊的$YGH$秒掉上面两道题之后,开始思考有趣的回文串问题了. 他面前就有一个漂浮着的字符串.显然$YGH$是会$manacher$的,于是他随手求出了这个字符串的回文子串个数.但是他 ...
- jsplumb+dragable+vue(一)
基于vue的jsplumb,支持拖拽生成节点,节点双击展示更多信息,节点连线,删除节点,删除连线,重绘连接图,当前页面刷新连接图,根据json画连接图等功能 本章主要讲 拖拽生成节点 获取链接图的信息 ...
- list_car()函数小记
一 ,list_car ,前端传过来参数字典,从字典中获取参数 二, 根据参数去数据库中查找,条件查找 三,将查找出来的对象,flask_sqlalchemy.BaseQuery,然后通过这个对象的 ...
- 金山云无法ping通外网
解决方法:在网络安全组中放行ping端口.
- DEDE网站地图优化技巧
DEDE网站地图优化技巧-把网站地图生成在系统根目录,利于搜索引擎收录相信恨多用DEDECMS做站的朋友,为避免将data目录内的东西随便外泄,在robots中将data目录屏蔽了,但是DEDE默认的 ...
- poj1182食物链(三类并查集)
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种 ...
- HTTP权威指南-学习笔记
目录 HTTP权威指南-学习笔记 HTTP: Web的基础 URL与资源 HTTP报文 连接管理 HTTP结构 Web服务器 代理 缓存 集成点: 网关,隧道及中继 Web机器人 识别,认证与安全 客 ...
- centos7系统乱码问题解决
操作步骤: 查看当前系统的默认语言 echo $LANG 查看系统支持的语言库 locale 如果没有要设置的语言需要安装一下 yum groupinstall chinese-support -y ...