1011. Capacity To Ship Packages Within D Days**

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: 10Note 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

C++ 实现 1

本题思想有了, 但不成熟, 最后通过阅读 [Java/C++/Python] Binary Search 求解.

需要注意的是, days 初始化值需要是 1, 这是因为: if (part_weights + weights[i] > capacity) 这个判断语句其实隐含的含义是 part_weights 当前是小于或等于 capacity 的, 就像上面的 1 + 2 + 3 + 4 <= 10, 现在要判断加上了 weights[i] = 5 是否会超过 capacity, 就算不会超过, 那也要一天将货物运完, 故 days 初始值设置为 1.

Binary Search 找到第一个小于或等于的 Ddays. 注意要对 left 初始值约束为货物中的最大值. 如果没有这个约束, 代码会不正确. 原因在于, 比如 left 如果初始化为 0 的话, 可能搜索到的 capacity 比较小, 这样会用更多的 days 来运送货物, 但是 capacity 怎么能小于 weights 中的最大值呢?

class Solution {public:int shipWithinDays(vector<int>& weights, int D) {int left = 0, right = 0;for (auto &w : weights) {right += w;left = std::max(left, w); // 要限定船的重量至少是 weights 中的最大值}int res = right;while (left <= right) {int capacity = left + (right - left) / 2;int part_weights = 0;int days = 1;for (int i = 0; i < weights.size(); ++ i) {if (part_weights + weights[i] > capacity) {days += 1;part_weights = 0;}part_weights += weights[i];}if (days > D) left = capacity + 1; // 倘若花费的时间太多, 应该增加容量else right = capacity - 1; // 否则减少容量}return left;}
};

1011. Capacity To Ship Packages Within D Days**相关推荐

  1. LeetCode 1011. Capacity To Ship Packages Within D Days(python)

    1011. Capacity To Ship Packages Within D Days Capacity To Ship Packages Within D Days python solutio ...

  2. Leetcode 1011. Capacity To Ship Packages Within D Days 在 D 天内送达包裹的能力

    Leetcode 1011. Capacity To Ship Packages Within D Days 在 D 天内送达包裹的能力 1011. Capacity To Ship Packages ...

  3. LeetCode #1011. 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  ...

  4. 1011. Capacity To Ship Packages Within D Days

    题目链接:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 解题方法:此题参考博客,遇到序列计算时,利用两个 ...

  5. leetcode 1014. Capacity To Ship Packages Within D Days

    leetcode 1014. Capacity To Ship Packages Within D Days 题意:给你一个数组,表示一堆货的重量.再给你一个数d,表示要求运货的天数.求一个最小的载重 ...

  6. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  7. LeetCode 二分查找

    文章目录 [0378. 有序矩阵中第K小的元素 [Medium] [Kth Smallest Element in a Sorted Matrix]](https://leetcode.com/pro ...

  8. Arithmetic_Thinking -- greedy algorithm

    贪心算法--就是一种寻找局部最优解的情况,然后整合成整体最优解的情况 简单的例子:买菜的找钱,现在有1元,5角,1角的硬币,要找给别人2元7角,现在是怎么才能以最少的硬币量找给别人,肯定是先来两个1元 ...

  9. Linux schedule 4、负载均衡

    4.负载均衡 4.1.SMP负载均衡 4.1.1.Scheduling Domains 4.1.1.1.Scheduling Domains概念 借用Linux Scheduling Domains的 ...

最新文章

  1. inline hook __usercall 函数
  2. 段式存储管理 Vs 页式存储管理 Vs 段页式存储管理
  3. 1371. Find the Longest Substring Containing Vowels in Even Counts
  4. word文本样式代码样式_使用文本样式表达创建真相来源
  5. mysql-5.7.10-winx64 MySQL服务无法启动,服务没有报告任何错误的解决办法
  6. 中位数及带权中位数问题(转)
  7. nolo手柄配对不上_nolo手柄连接不上
  8. freeswitch 自定义application
  9. linux下python网络编程框架-twisted安装手记,linux下Python网络编程框架-Twisted安装
  10. chrome扩展程序_如何在20分钟内创建和发布Chrome扩展程序
  11. 使用OpenSSL模拟SSL证书验证过程
  12. sql查询重复订单号
  13. 文本输入框input实现字母大小写转换
  14. 全网通蜂窝路由器_工业路由器双sim卡
  15. 输入一串只有由1-9对应数字的拼音输出对应的数字如输入为yiersan输出为:123
  16. html表格上下居中
  17. 《牛津字典精华总结》- 初阶系列 - 字母 - S
  18. 高度设置php,uedit设置固定高度
  19. 分布式消息队列RocketMQ与Kafka的18项差异之“拨乱反正”
  20. Meteor 初级入门 三

热门文章

  1. 【函数式】Monads模式初探——Monad概念
  2. 仰望星空脚踏实地:技术人如何自我成长?
  3. 远程管理软件(xshell)介绍和系统连接
  4. 傲娇大少之---【JS的原型,prototype、__proto__、constructor】
  5. SWAN之ikev2协议forecast配置测试
  6. 中小学必背的208篇古诗文
  7. 赣州服务器系统,赣州服务器费用
  8. BZOJ3039-玉蟾宫
  9. fcfs和sjf java_Java简单实现进程调度算法 FCFS和SJF
  10. 巨人之后的史蒂夫·鲍尔默和蒂姆·库克