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 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 <= D <= weights.length <= 500001 <= weights[i] <= 500
分析: binary search
class Solution {
public int shipWithinDays(int[] weights, int D) {
int left = , right = ;
for (int w: weights) {
left = Math.max(left, w);
right += w;
}
while (left <= right) {
int mid = (left + right) / , need = , cur = ;
for (int w: weights) {
if (cur + w > mid) {
need += ;
cur = ;
}
cur += w;
}
if (need > D) left = mid + ;
else right = mid - ;
}
return left;
}
}
Capacity To Ship Packages Within D Days的更多相关文章
- Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)
Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...
- [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 ...
- 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 ...
- 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. Capacity To Ship Packages Within D Days
原题链接在这里:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 题目: A conveyor belt h ...
- 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 ...
- 【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 ...
- 【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: ...
随机推荐
- printf:函数参数计算从右向左,从左向右?
造冰箱的大熊猫@cnblogs 2019/8/3 1.问题 某天写了如下代码: unsigned char ReadByteFromFile ( FILE * fp ) { unsigned char ...
- 单调栈求全1(或全0)子矩阵的个数 洛谷P5300与或和 P3400仓鼠窝
爆零好爽,被中学生虐好爽,还好我毕业得早 求全1(或全0)子矩阵的个数,看了题解有好几种思路,我学了三种,但有两种不是很理解,而且也没另外那个跑得快,所以简单讲述一一下我会的那种来自Caro23333 ...
- HGOI 20191031am 题解
Problem A Divisors 给出$m$个不同的正整数$a_i$,设数论函数 $f(k) = \sum\limits_{i = 1}^{n} [(\sum\limits_{j = 1}^ ...
- 【转载】最小生成树之Kruskal算法
给定一个无向图,如果它任意两个顶点都联通并且是一棵树,那么我们就称之为生成树(Spanning Tree).如果是带权值的无向图,那么权值之和最小的生成树,我们就称之为最小生成树(MST, Minim ...
- 两个字说清楚编程语言实质-Python基础前传(3)
大家都知道Python对于数据分析而言,意义重大.但对于运用Python来做数据分析的我们来说,学习一门编程语言,却有不小的难度:首先大部分伙伴不是计算机相关专业的科班出身,其次我们基本上也都没有程序 ...
- HTTP第八、九章之网关、隧道、web机器人
网关 网关(gateway): 资源和应用程序之间的粘合剂.应用程序可以(通过HTTP或其它已定义的接口)请求网关来处理某条请求,网关可以提供一条响应.网关可以向数据库发送查询语句,或者生成动态的内容 ...
- OVS报错:s1.mgmt: version negotiation failed (we support version 0x01, peer supports version 0x04) ovs-ofctl: s1: failed to connect to socket (Broken pipe)
mininet搭建拓扑后,查看流表,发现ovs命令报错 查找原因发现系mininet创建拓扑的命令使用了OpenFlow 1.3版本 ovs dump-flows命令默认是1.0版本,因此需要在ovs ...
- django 快速搭建blog(三)
http://www.cnblogs.com/fnng/p/3737964.html 引用自此博客 创建blog的公共部分 从Django的角度看, 一个页面 具有三个典型的组件: 一个模板(temp ...
- 预处理、const、static与sizeof-用宏定义得到一个字的高位和低位字节
1:代码如下: #define WORD_LO(XXX) ((byte) (word)(XXX) & 255) #define WORD_HI(XXX) ((byte) (word)(XXX) ...
- Ajax/Highcharts—动态图表
前面写过“Highcharts的用法总结”,当然了,在实际应用中,图表数据都是要从后台获取的,根据之前的使用,贴一些例子来分享学习. 首先,如果没有获取后台数据,又希望呈现一个动态图表的话,可以很轻易 ...