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

题意看错了,应该是找最大值。这样妥妥的二分啊

当sum>max时候,我们开始二分。

疯狂找(ai+....+ax) > mid 这个有几段 getRequiredPainters这个就这么用的

int getMax(int A[], int n) {
int max = INT_MIN;
for (int i = ; i < n; i++) {
if (A[i] > max) max = A[i];
}
return max;
} int getSum(int A[], int n) {
int total = ;
for (int i = ; i < n; i++)
total += A[i];
return total;
} int getRequiredPainters(int A[], int n, int maxLengthPerPainter) {
int total = , numPainters = ;
for (int i = ; i < n; i++) {
total += A[i];
if (total > maxLengthPerPainter) {
total = A[i];
numPainters++;
}
}
return numPainters;
} int BinarySearch(int A[], int n, int k) {
int lo = getMax(A, n);
int hi = getSum(A, n); while (lo < hi) {
int mid = lo + (hi-lo)/;
int requiredPainters = getRequiredPainters(A, n, mid);
if (requiredPainters <= k)
hi = mid;
else
lo = mid+;
}
return lo;
}
class Solution {
public:
int shipWithinDays(vector<int>& weights, int D) {
int num[] = {};
int cnt = ;
int len = weights.size();
for(int i = ; i < len ; i++){
num[i] = weights[i];
}
return BinarySearch(num ,len, D);
}
};

128th LeetCode Weekly Contest Capacity To Ship Packages Within D Days的更多相关文章

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

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

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

  3. 128th LeetCode Weekly Contest Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  4. 128th LeetCode Weekly Contest Complement of Base 10 Integer

    Every non-negative integer N has a binary representation.  For example, 5 can be represented as &quo ...

  5. Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)

    Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...

  6. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

随机推荐

  1. sql语句表连接

    "Persons" 表: Id_P LastName FirstName Address City 1 Adams John Oxford Street London 2 Bush ...

  2. SQL Server数据库大型应用解决方案总结(转)

    出处:http://tech.it168.com/a2012/0110/1300/000001300144.shtml [IT168 技术]随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计 ...

  3. HttpUploader6-queue版本更新说明

    HttpUploader6-queue版本更新说明 博客园:http://www.cnblogs.com/xproer/p/5109761.html 网易博客:http://hyhyo.blog.16 ...

  4. 编写高质量代码改善C#程序的157个建议——建议91:可见字段应该重构为属性

    建议91:可见字段应该重构为属性 字段和属性的本质区别就是属性是方法. 查看下面这个Person类型: class Person { public string Name { get; set; } ...

  5. windows在python基础上安装pip

    首先你必须已经安装了python,并且配置好环境 键入pip 复制https://bootstrap.pypa.io/get-pip.py的内容并创建get-pip.py文件(该文件的内容就是刚刚复制 ...

  6. Python3常见Exception

    异常                                     描述BaseException                    新的所有异常类的基类Exception        ...

  7. 洛谷P4174 [NOI2006]最大获利(最大流)

    题目描述 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU 集团旗下的 CS&T 通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需 ...

  8. Delphi IOS开发环境安装

    RAD Delphi XE/10 Seattle 安装IOS.OSX环境安装,IOS模拟器,MAC X 真机可以调试 http://community.embarcadero.com/blogs/en ...

  9. 7z文件格式及其源码的分析(五)

    这是7z文件格式及其源码的分析系列的第五篇. 上一篇讲到了7z文件压缩流程.最近太忙了,好久没更新,都快忘了写到哪了.:) 这一篇就说说7z文件的尾头的生成方式吧. 上一篇已经讲了尾header的结构 ...

  10. Linux Qt 5.x 环境搭建

    Step 1 从Qt官网下载 qt-opensource-linux-x64...run 在linux命令行中给予文件可执行权限 $ chmod u+x qt-opensource-linux...r ...