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. yum源仓库的三种搭建方式

    yum源的三种搭建方式 一.  本地yum仓库的搭建 1.1.获取软件包资源 将iso镜像挂载在本地目录中,此次挂载目录为/var/www/html/repo/,此目录本身不存在,需要创建.软件宝资源 ...

  2. java连接mysql数据库时的时区设置问题(time_zone)

    java在连接mysql数据库时,会由于时区设置不正确导致报以下的错误:   The server time zone value '???ú±ê×??±??' is unrecognized or ...

  3. Easy sssp(spfa判负环与求最短路)

    #include<bits/stdc++.h> using namespace std; int n,m,s; struct node{ int to,next,w; }e[]; bool ...

  4. 「CQOI2015」选数

    「CQOI2015」选数 题目描述 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都 ...

  5. call与apply的用法

    call与apply的作用: 1.改变函数内部this指向 2.实现函数继承 代码演示: var obj={dividend:2}; function isOdd(num){ var result = ...

  6. DELL--R420 CPU报警“CPU0000 cpu2 internal error (IERR)contact support”

    按照以下操作解决: 请用户按以下操作,搞定. 请使用一台服务器测试: 开机看到dell标志时,按F2键→"System BIOS Setting" →"System Pr ...

  7. ES的底层原理-倒排索引的概念

    Elasticsearch底层使用的使用的lucene lucene使用的是倒排索引的方式来进行加快检索速度 倒排索引的原理 doc_1      The quick brown fox jumped ...

  8. MySQL kill进程后出现killed死锁问题

    公司同事删除一张大表的数据,本想直接drop表,但是使用了delete删除表,发现很慢,就kill了这个操作, 但是,kill后,表锁住了,因为在回滚表数据. 原文链接:https://blog.cs ...

  9. 【mysql】连接和断开服务器

    [mysql]连接和断开服务器 #启动服务: $sudo service mysql start #停止服务: $sudo service mysql stop 要连接到服务器,我们通常需要提供MyS ...

  10. fgets()函数

    声明: char *fgets(char *str,int n,FILE* stream) 参数: str—这是指向一个字符数组的指针,该数组存储了要读取的字符串 n – 这是要读取的最大字符数(包括 ...