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. yolov3---报"段错误"的可能原因及解决方法

    参考: ************************************************************************************************ ...

  2. 为什么将项目托管到Apache,浏览器输入http://127.0.0.1会跳转到http://127.0.0.1//dashboard/?

    找到xampp安装的根目录下htdocs文件夹下的index.php文件 <?php if (!empty($_SERVER['HTTPS']) && ('on' == $_SE ...

  3. 牛客82-B:区间的连续段 (ST表,贪心)(WXK牛逼)

    题目描述 给你一个长为n的序列a和一个常数k 有m次询问,每次查询一个区间[l,r]内所有数最少分成多少个连续段,使得每段的和都 <= k 如果这一次查询无解,输出"Chtholly& ...

  4. redis堵死致数据清空

    情景: zy的链路监控突然都恢复,而且在哪个时间段zabbix中显示回复,也发送了告警,但是实际上告警并没有发出来.这是不可能的情况,应该是redis缓存中的数据都被清空了,没有认为干预,需解决问题 ...

  5. 40、JSON数据源综合案例实战

    一.JSON数据源综合案例实战 1.概述 Spark SQL可以自动推断JSON文件的元数据,并且加载其数据,创建一个DataFrame.可以使用SQLContext.read.json()方法,针对 ...

  6. UOJ46 【清华集训2014】玄学 【时间线段树】

    题目链接:UOJ 这题的时间线段树非常的妙. 对时间建立线段树,修改的时候在后面加,每当填满一个节点之后就合并进它的父亲. 对于一个节点维护序列,发现这是一个分段函数,合并就是归并排序.于是就形成了差 ...

  7. php-fpm 参数调优

    php-fpm 进程池优化方法 php-fpm进程池开启进程有两种方式,一种是static,直接开启指定数量的php-fpm进程,不再增加或者减少:另一种则是dynamic,开始时开启一定数量的php ...

  8. PowerBuilder 这么古老的语言(破解一软件)

     PowerBuilder 这么古老的语言,编辑器用的6.5的好古老的气息,好吧破解木有兴趣了, 不过嘛可以说一下破解思路,这个系统使用的是圣天狗,联网版的. 复制狗(暴力,没技术味道) 模拟狗(也是 ...

  9. Pytest权威教程01-安装及入门

    目录 安装及入门 安装 Pytest 创建你的第一个测试用例 执行多条测试用例 断言抛出了指定异常 使用类组织多条测试用例 函数测试中请求使用独立的临时目录 进一步阅读 返回: Pytest权威教程 ...

  10. bytearray 字符串转为字节

    >>> str="hello world">>> x=bytearray(str)>>> xbytearray(b'hello ...