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. Python爬虫入门二之爬虫基础了解

    1.什么是爬虫 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来.想抓取什么?这个由你来控制它咯. ...

  2. org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions:323) | Loading XML bean definitions from class path resource [

    今天遇到一个这样的错误,这个错误是说我的spring的框架的文档没有写正确.但是反复检查,文档没有错误,原因是我使用了自己只做的user library,而且使用了 下边的System library ...

  3. 简单引入logback

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  4. ADF文件数据结构解析和ADF文件读写

    包括位姿和特征点位置和描述信息. What does an Area Description File (ADF) looks like? 4down votefavorite 2 I'm start ...

  5. JavaScript - this详解 (一)

    侃侃JavaScript中的this this为何如此多变? this总是跟它的执行上下文有关,而在JavaScript总会有开辟新的执行上下文的东西,比如函数,所以,this才如此的变化. 执行环境 ...

  6. MFC中cstring,string和char[]的相互转化

    int 转 CString:CString.Format("%d",int);...............................string 转 CString CSt ...

  7. ArcGis Android 10.2.6更新文档翻译

    ArcGis Android 10.2.6更新文档翻译 @[ArcGis Android|10.2.6|更新文档] 本文描述了ArcGIS Runtime SDK for Android 10.2.6 ...

  8. window.open之postMessage传参数

    这次要实现一个window.open打开子视窗的同时传参数到子视窗,关闭的时候返回参数. 当然简单的做法非常简单,直接在window.open的URL之后接参数即可,但是毕竟get method的参数 ...

  9. Conditional Expressions

    Conditional Expressions建立一些逻辑关系 The conditional expression classes from django.db import models clas ...

  10. .net Timer定时执行

    System.Timers.Timer可以实现数据库定时更新的功能 Global.asax void Application_Start(object sender, EventArgs e) { / ...