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

分析: binary search

 class Solution {
public int shipWithinDays(int[] weights, int D) {
int left = , right = ;
for (int w: weights) {
left = Math.max(left, w);
right += w;
}
while (left <= right) {
int mid = (left + right) / , need = , cur = ;
for (int w: weights) {
if (cur + w > mid) {
need += ;
cur = ;
}
cur += w;
}
if (need > D) left = mid + ;
else right = mid - ;
}
return left;
}
}

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

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

  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 1011. Capacity To Ship Packages Within D Days

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

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

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

  8. 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

随机推荐

  1. mobx是什么?有什么优点?

    mobx是一个简单可扩展的状态管理库. mobx vs redux mobx是学习成本更低,性能更好的状态解决方案. mobx开发难度低: mobx代码量少: mobx渲染性能好: mobx参考

  2. [Luogu] 维护序列

    https://www.luogu.org/problemnew/show/P2023 线段树双懒标记下放 #include <bits/stdc++.h> using namespace ...

  3. Python面试题:使用栈处理括号匹配问题

    括号匹配是栈应用的一个经典问题, 题目 判断一个文本中的括号是否闭合, 如: text = "({[({{abc}})][{1}]})2([]){({[]})}[]", 判断所有括 ...

  4. SecureCRT的安装与破解,详细过程

    使用SecureCRT可以方便用户在windows环境下对linux主机进行管理,这里为大家讲一下SecureCRT的破解方法,仅供大家参考学习: 1.首先在本站下载SecureCRT 7.3.6最新 ...

  5. mysql:unknown variable 'default-character-set=utf8'

    1.修改my.cnf后,执行 service mysql restart 重启数据库失败 service mysql restart Shutting down MySQL.. SUCCESS! St ...

  6. Leetcode题目分类整理

    一.数组 8) 双指针 ---- 滑动窗口 例题: 3. Longest Substring Without Repeating Characters 描述:Given a string, find ...

  7. HTTP之Cookie和Session

    1. Cookie 1.1 为什么需要 Cookie? HTTP 协议是一种无状态的协议,也就是说,当前的 HTTP 请求与以前的 HTTP 请求没有任何联系.显然,这种无状态的情形在某些时候将让用户 ...

  8. 第11组 团队Git现场编程实战

    第11组 团队Git现场编程实战 组员职责分工: 前端部分: 陈郑铧:构架的搭建,前端模块开发 陈益:前端模块开发 李镇平:前端模块开发 后端部分: 沈国煜:后端模块开发 王泽鸿:后端模块开发 林铮威 ...

  9. Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效)

    Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效) 一.总结 一句话总结: 1.卸载自己机器上面的Navicat,安装下载的包里面的Navicat安装包,不然可能不行 2. ...

  10. Redis 的配置

    Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法 Redis CONFIG 命令格式如下: redis 12 ...