LeetCode 1011. Capacity To Ship Packages Within D Days
原题链接在这里:https://leetcode.com/problems/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
题解:
For the possible weight capacity, it must be between max(weights) and sum(weights).
Because, if it is smaller than weights[i], ship could never carry i.
When it is sum(weights), it could ship all in 1 day.
Binary search with a guess mid.
Accumate weight from beginning, when sum + w > mid, need a new ship. count of ships plus 1. sum = 0.
If count > D, then it is late. Need to guess bigger, l = mid+1.
Otherwise, it is okay. But it could smaller, r = mid.
Time Complexity: O(nlogm). n = weights.length. m = sum(weights).
Space: O(1).
AC Java:
class Solution {
public int shipWithinDays(int[] weights, int D) {
if(weights == null || weights.length == 0){
return 0;
} int l = 0;
int r = 0;
for(int w : weights){
l = Math.max(l, w);
r += w;
} while(l<r){
int mid = l + (r-l)/2;
int count = 1;
int sum = 0;
for(int w : weights){
if(sum + w > mid){
count++;
sum = 0;
} sum += w;
} if(count > D){
l = mid + 1;
}else{
r = mid;
}
} return l;
}
}
类似Minimize Max Distance to Gas Station, Koko Eating Bananas.
LeetCode 1011. Capacity To Ship Packages Within D Days的更多相关文章
- Leetcode 1014. Capacity To Ship Packages Within D Days
二分搜索 class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: ...
- Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)
Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...
- 【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: 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 ...
- [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基础教程_查询价格
少儿编程和数学是有这一定联系的,若是将编程弄好了,数学成绩也就会有所提高,今天就为大家来介绍一下一个关于数学的相关编程,现在我们就来看看吧! 题目描述 编程实现以下功能:查询水果的单价.有4种水果,苹 ...
- (六)pdf的构成之文件体(pages对象)
页面树(pages) 通过页面树访问文档的页面,页面树定义PDF文档中的所有页面.树包含表示PDF文档页面的节点,可以是两种类型:中间节点和叶节点.中间节点也称为页面树节点,而叶节点称为页面对象.最简 ...
- OpenResty部署nginx及nginx+lua
因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件 ...
- C#——零散学习1
C#——零散学习1 //结构体(与C语言相似) struct Position { public float x; public float y; //不一定需要把结构体成员设置为pu ...
- Python的virtualenv管理
原文链接 虚拟环境 Python 开发中所谓的虚拟环境,就是为 Python 版本及第三方库创建独立的开发环境,使不同项目之间互不干扰.借助于虚拟环境,我们可以在同一台电脑上构建出项目 A 在基于 P ...
- python-tyoira基本
目录 .Typora安装 我们在之前的时候记录笔记就是使用word和记事本,但是从今天开始我们要更换软件,记录笔记使用Typora软件,为什么要使用Typora的软件呢,是因为我们程序员不只是写代码这 ...
- Java自学-数组 创建数组
Java 如何创建一个数组 数组是一个固定长度的,包含了相同类型数据的 容器 步骤 1 : 声明数组 int[] a; 声明了一个数组变量. []表示该变量是一个数组 int 表示数组里的每一个元素都 ...
- MySQL数据库汇总
-- mysql的最大连接数:默认为 100 -- mysql的增删改查 -- mysql统计各个字段(case when 用法 注:也可以使用其他的) select (case when ...
- .python3基础之“术语表(1)”
1.注释: 行首有一特殊标志符号运行时告知编程忽略此行:使代码更易于阅读. 例如: #这是一个注释 print("hello world") #print() 方法用于打印输出,p ...
- JMeter学习笔记(十八)——返回的响应数据出现中文乱码_解决方案
一.问题描述 使用jmeter过程中遇到了请求返回的响应数据出现中文乱码 二.原因分析 当没有对响应数据or响应页面设置支持解析中文的编码时,JMeter则会以默认的ISO-8859-1格式解析,而其 ...