原题链接在这里: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. 1 <= D <= weights.length <= 50000
  2. 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 StationKoko Eating Bananas.

LeetCode 1011. Capacity To Ship Packages Within D Days的更多相关文章

  1. Leetcode 1014. Capacity To Ship Packages Within D Days

    二分搜索 class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: ...

  2. Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)

    Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...

  3. 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 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 ...

  5. 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 ...

  6. 【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  ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Anaconda的pip加速下载命令

    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

  2. C++中const限定符

    const基础 C++中的const,用于定义一个常量,这个常量的值不能被修改.因为const对象一旦创建就不能修改,所以const对象必须初始化.const常量特征仅仅在执行改变其本身的操作时才会发 ...

  3. 2019-7-01 python基础数据类型

    一.python的注释 python的注释分类: 单行注释:  #    单行注释 多行注释:(可以是三个单也可以是三个双) ''' 单三引号多行注释 ''' """ 双 ...

  4. Python之路【第九篇】:Python面向对象

    阅读目录 一.三大编程范式 编程范式即编程的方法论,标识一种编程风格: 大家学习了基本的python语法后,大家可以写python代码了,然后每个人写代码的风格不同,这些不同的风格就代表了不同的流派: ...

  5. Windows 编译安装 nginx 服务器 + rtmp 模块

    有关博客: <Windows 编译安装 nginx 服务器 + rtmp 模块>.<Ubuntu 编译安装 nginx>.<Arm-Linux 移植 Nginx> ...

  6. NOI2019 退役记

    最终还是在意料之中退役了. 总的来说,这一年确实曲折坎坷,曾踏足山巅,也曾陷入低谷,二者都让我受益良多. 没有太多不甘,水平已经正常发挥,哪敢还有一丝奢求. 省选时其实已经早就做好退役的准备了,但命运 ...

  7. 移动端开发rem布局之less+媒体查询布局的原理步骤和心得

    rem即是以html文件中font-size的大小的倍数rem布局的原理:通过媒体查询设置不同屏幕宽度下的html的font-size大小,然后在css布局时用rem单位取代px,从而实现页面元素大小 ...

  8. Kafka重启出错:Corrupt index found

    日志记录 FATAL Fatal error during KafkaServerStable startup. Prepare to shutdown (kafka.server.KafkaServ ...

  9. C#类类型

    一.类和对象 假设我开了一家烤鱼店,每当客人来点餐时,我就会用笔和纸记录这笔订单,并计算出每单的价格.以下是记录的订单: 单号:00001种类:清江鱼口味:香辣配菜:豆腐价格:140元-------- ...

  10. tf.reduce_mean函数用法及有趣区别

    sess=tf.Session() a=np.array([1,2,3,5.]) # 此代码保留为浮点数 a1=np.array([1,2,3,5]) # 此代码保留为整数 c=tf.reduce_m ...