原题链接在这里: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. Codeforces Round #603 (Div. 2) (题解)

    A. Sweet Problem (找规律) 题目链接 大致思路: 有一点瞎猜的,首先排一个序, \(a_1>a_2>a_3\) ,发现如果 \(a_1>=a_2+a_3\) ,那么 ...

  2. STM32 EV1527无线通信(433)

    EV1527无线通信 先说一下这个通信协议的数据格式,这个图片是我在手册里截的. 大家按照单片机类型计算周期,我的是STM32f103vb (4CLK大致等于350um) 发送时按照 先发同步码后发D ...

  3. flink checkpoint状态储存三种方式选择

    Flink 提供了三种可用的状态后端:MemoryStateBackend,FsStateBackend,和RocksDBStateBackend. MemoryStateBackend Memory ...

  4. 阿里巴巴 Java 开发手册 (五) 集合处理

    1. [强制]关于 hashCode 和 equals 的处理,遵循如下规则: 1) 只要重写 equals,就必须重写 hashCode. 2) 因为 Set 存储的是不重复的对象,依据 hashC ...

  5. selenium中的元素操作之三大等待(一)

    等待时做什么,为什么使用等待 在做自动化测试,设计测试用例的时候,有时下一步的操作会依赖上一步的结果或者内容,上一步操作成功之后才能进行下一步操作等,这时候,我们就需要使用等待,来判断上一步操作是否完 ...

  6. Java调用Http/Https接口(4)--HttpClient调用Http/Https接口

    HttpClient是Apache HttpComponents项目下的一个组件,是Commons-HttpClient的升级版,两者api调用写法也很类似.文中所使用到的软件版本:Java 1.8. ...

  7. 最全的 pip 使用指南,50% 你可能没用过

    所有的 Python 开发者都清楚,Python 之所以如此受欢迎,能够在众多高级语言中,脱颖而出,除了语法简单,上手容易之外,更多还要归功于 Python 生态的完备,有数以万计的 Python 爱 ...

  8. Vue学习之webpack中使用vue(十七)

    一.包的查找规则: 1.在项目根目录中找有没有 node_modules 的文件夹: 2.在 node_modules 中根据包名,找对应的vue 文件夹: 3.在vue 文件夹中,找 一个叫做 pa ...

  9. Vue编程式跳转

    编程式跳转 <template> <ul class = "prolist"> <!-- //产品 --> <!-- :to = &quo ...

  10. TOEFL词汇笔记英语托福英语

    conjectural-based on guessing 推测的-给予猜测的 consciously-on purpose 有意识地-有目的地 conserve-retain保存-保存 conspi ...