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的更多相关文章

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

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

  2. LeetCode 1011. Capacity To Ship Packages Within D Days

    原题链接在这里:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 题目: A conveyor belt h ...

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

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

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

  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. ubuntu---对比工具Meld

    Beyond Compare是商业软件,下载地址:http://www.scootersoftware.com/download.php.下载完直接运行或者通过dpkg安装即可. 其实Linux下文本 ...

  2. vim、gvim 在 windows 下中文乱码的终极解决方案

    vim.gvim 在 windows 下中文乱码的终极解决方案 vim ~/.vimrc 然后加入: " Gvim中文菜单乱码解决方案 " 设置文件编码格式 set encodin ...

  3. 【转】Rxjs知识整理

    原文:https://www.jianshu.com/p/16be96d69143 ---------------------------------------------------------- ...

  4. 《你们都是魔鬼吗》第八次团队作业:第一天Alpha冲刺

    <你们都是魔鬼吗>第八次团队作业:Alpha冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 完成最 ...

  5. Office2016专业增强版永久激活

    Office2016专业增强版永久激活码:Microsoft Office 2016 Pro Plus Retail Mak序列号XNTT9-CWMM3-RM2YM-D7KB2-JB6DVBHXN7- ...

  6. Java - 框架之 Hibernate

    一:hibernate.cfg.xml 配置 <!-- 1.配置数据库连接的4个参数 --> <property name="hibernate.connection.dr ...

  7. 【shell】1、变量的声明、引用及作用域

    shell程序 以文件形式存放==批量的Linux命令集合==,该文件能够被Shell解释执行,这种文件就是Shell脚本程序 通常由一段Liunx命令.Shell命令.控制语句以及注释语句构成 Sh ...

  8. C# 异步编程(async&await)

    同步:同步就是指一个进程在执行某个请求的时候,若该请求需要一段时间才能返回信息,那么这个进程将会一直等待下去,直到收到返回信息才继续执行下去 异步:异步是指进程不需要一直等下去,而是继续执行下面的操作 ...

  9. 边框图片border-image

    一.定义: 在内容变化的容器里使用,边框自动填充,由于浏览器的兼容问题,没有广泛使用 border-image属性是速记属性用于设置 border-image-source, border-image ...

  10. 2-使用git管理一个单片机程序

    https://www.cnblogs.com/yangfengwu/p/10842205.html 我用电脑压缩一个文件,然后通过git上传,然后在新买的linux系统通过wget 网络下载这个压缩 ...