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 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 <= 50000
1 <= weights[i] <= 500
Exactly the same with Split Array Largest Sum. Binary search or DP
Binary Search
class Solution {
public int shipWithinDays(int[] weights, int D) {
int sum = 0;
int max = -1;
for (int w : weights) {
sum += w;
max = Math.max(max, w);
} int l = max, r = sum;
while (l <= r) {
int m = l + (r - l) / 2;
if (isEligible(weights, m, D)) {
r = m - 1;
}
else l = m + 1;
}
return l;
} public boolean isEligible(int[] weights, int k, int D) {
int count = 1;
int cur = 0;
for (int w : weights) {
cur += w;
if (cur > k) {
cur = w;
count ++;
if (count > D) return false;
}
}
return true;
}
}
Leetcode: 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 天内从一个港口运送到另 ...
- 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】1014. Capacity To Ship Packages Within D Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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】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
二分搜索 class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: ...
- [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 ...
- 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 ...
随机推荐
- 【编程开发】Python隐藏属性——使用双下划线标识私有属性,外部不可直接访问
from:https://zhuanlan.zhihu.com/p/30553607 小编在最初使用上Python之后,就一发不可收拾,人生苦短.我用Python,不光是因为其优雅简洁, ...
- selenium常用的API(一)截屏
我们在使用selenium测试过程中,可使用截屏功能将用例执行失败的画面截图保存,方便测试执行结束后查看并定位问题. 以下介绍两种截屏方法: 对当前浏览器窗口截屏 使用selenium自带的get_s ...
- 0029redis单机版环境搭建
linux环境下安装单机版redis,主要分为如下几步: 1. 安装gcc 2.下载安装包 3.解压安装包 4.进入解压目录并执行make和make install命令 5.查看默认安装目录 6.更改 ...
- LG3768 简单的数学题
P3768 简单的数学题 题目描述 输入一个整数n和一个整数p,你需要求出$(\sum_{i=1}^n\sum_{j=1}^n ijgcd(i,j))~mod~p$,其中gcd(a,b)表示a与b的最 ...
- Flask - 请求响应 | session | 闪现 | 请求扩展 | 中间件
请求响应 flask的请求信息都在request里 flask的响应方式有四剑客,也可以自定义响应 请求相关信息 # request.method 提交的方法 # request.args get请求 ...
- c语言实现整数转换为字符串——不考虑负数
#include <stdio.h> #include <string.h> #define MAX_LEN 16 #define ESP 1e-5 typedef int i ...
- C++异常处理(一)----基本语法
实验环境 win7 下的vs2017,基本原则:throw抛出的数据类型,和cathc语句的数据类型要一致 异常的引发和异常的处理可以分布在不同函数中,所以c++的异常是跨栈的 异常是由“地地道道“的 ...
- Openwrt路由器上安装python
在路由器安装python之前,还是经过了一番折腾的.淘宝上买了个已经刷好系统的小米迷你路由器,但里面安装的不是预期的Pandorbox,而是LEDE. 这个固件已经带了大量自带的软件,128的内存实在 ...
- C语言for 循环 9*9 实现九九乘法表
#include <stdio.h> int main(void) { //for循环实现9*9乘法表 /* 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 */ ...
- Codeforces 1172F Nauuo and Bug [线段树]
Codeforces 思路 定义\(f_{l,r}(x)\)表示数\(x\)从\(l\)进去\(r\)出来的时候会变成什么样子.容易发现这个函数是个分段函数,每一段都是斜率为1的一次函数,并且段数就是 ...