原题链接在这里: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. 迪杰斯特拉算法(Dijkstra) (基础dij+堆优化) BY:优少

    首先来一段百度百科压压惊... 迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最 ...

  2. day54——jquery补充、bootstrap

    day54 jquery 页面载入 window.onload: 原生js的window.onload事件:// onload 等待页面所有内容加载完成之后自动触发的事件 window.onload ...

  3. [笔记] 命令行参数 int main(int argc,char *argv[])

    int main(int argc,char *argv[]) // argument count 变量个数 argument values 变量值 C程序的main函数有两个形参* argc:整数, ...

  4. sublime配置python环境及快捷键

    sublime配置python环境 参考链接:https://blog.csdn.net/VertigozZ/article/details/54574006 快捷键的配置:https://www.c ...

  5. Hibernate的关联映射--一对多、

    这是我 1 单向一对多: 实体类:(课程类)Grade与(学生类)Student的一对多关系 学生类: public class Student implements java.io.Serializ ...

  6. Dockerfile构建jar镜像

    dockerDockerfilejar包docker-compose 一.安装docker和compose 二.准备jar包 三.编写配置文件 1. Dockerfile 2. docker-comp ...

  7. 4_PHP流程控制语句_1_条件控制语句

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. PHP流程控制共有3种类型:条件控制结构.循环结构以及程序跳转和终止语句. 4.1 条件控制语句 4.1.1 if ...

  8. Python基础知识(八)----文件操作

    文件操作 一丶文件操作初识 ###f=open('文件名','模式',编码): #open() # 调用操作系统打开文件 #mode #对文件的操作方式 #encoding # 文件的编码格式 存储编 ...

  9. vue中is与:is的区别

    简略回答 假设父组件中有一个show数据,show="one":is="show"-->实际上是is="one" is="s ...

  10. 【强烈推荐】ok-admin 一个好看又好用的后台模版!!!

    ok-admin 一个很赞的,扁平化风格的,响应式布局的后台管理模版,旨为后端程序员减压! 目前一共有两个版本:ok-admin v1.0和ok-admin v2.0可自由选择! 源码地址:https ...