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 <= 500001 <= 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 ...
随机推荐
- opencv之重映射
好久没写呆码了 今天发个重映射 #include "opencv2/video/tracking.hpp" #include "opencv2/imgproc/imgpr ...
- Django框架(十一)--cookie和session
cookie和session组件 cookie 1.cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它 ...
- golang执行命令行(一)
golang中会经常遇到要 fork 子进程的需求.go 标准库为我们封装了 os/exec标准包,当我们要运行外部命令时应该优先使用这个库. 执行 command 这里我简单结合context 和 ...
- svg 直线水平渐变为什么没有效果,必须得是一条倾斜的不水平的直线才有渐变效果呢??
参考:https://blog.csdn.net/u012260672/article/details/80905631 对x1=x2(没有宽度)或者y1=y2(没有高度)的直线(line以及path ...
- mysql5.7新增加用户和授权
迁移mysql数据库,运行项目的时候发现nginx和uWSGI都配置正确,可就是网站打不开,看了log文件,发现错误: django.db.utils.OperationalError: (1044, ...
- Git diff (---和+++具体解释)(转)
转自:https://blog.csdn.net/lovezbs/article/details/46492933
- System.Web.NullPointerException
在.Net异步webApi中我们需要记录日志信息,需要获取客户端的ip地址,我们需要使用:HttpContext.Current.Request.ServerVariables["REMOT ...
- 如何在CentOS / RHEL 7上启用IPv6
默认情况下,在RHEL / CenOS 7系统上启用IPv6.因此,如果故意在系统上禁用IPv6,则可以通过以下任一方法重新启用它. 1.在内核模块中启用IPv6(需要重启)2.使用sysctl设置启 ...
- copy file
import io,,,,,,, from https://pub.dev/packages/large_file_copy Directory directory = await getApplic ...
- .net core 使用swagger接口描述
首先安装nuget包 Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerGen Swashbuckle.AspNetCore.S ...