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 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 <= D <= weights.length <= 50000
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的更多相关文章
- 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 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 ...
- 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 ...
- Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days)
Leetcode之二分法专题-1011. 在 D 天内送达包裹的能力(Capacity To Ship Packages Within D Days) 传送带上的包裹必须在 D 天内从一个港口运送到另 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
随机推荐
- 1710 生日蛋糕(1999 noi)
1710 生日蛋糕(1999 noi) 1999年NOI全国竞赛 题目描述 Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体 ...
- code1744 方格染色
稍微复杂一点的划分dp 设f[i][j][k]为第i行前j个k次粉刷正确的最大值 由于每行循环使用,可以去掉第一维,但每次不要忘了清零(卡了好久) f[j][k]=max{ f[u][j-1] + m ...
- hbase java api样例(版本1.3.1,新API)
hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...
- 给初学者的总结:jquery选择器
刚学jquery的时候是又渣又蠢的小白,而且把js和jquery混淆在一起. 把jquery的全部选择器总结在一起,才发现和css选择器好一部分都很像,并且有些选择器还很少用过. 我学习前端的路程是先 ...
- centos7部署JavaWeb项目
centos7部署JavaWeb项目共有三步 1.配置java环境 2.配置tomcat环境. 3.部署JavaWeb项目 一.配置java环境 1.1安装java 参考我的另一篇博文:https:/ ...
- Spring框架总结(七)
Spring代理模式:名词解释: 代理是一种开发的设计模式,用途:提供了对目标对象另外的访问方式,及通过对代理访问目标对象. 优势: 可以在目标对象实现的基础上,增强额外的功能操作,(扩展目标对象的功 ...
- java IO其他流
1.内存操作流,ByteArrayInputStream和 ByteArrayOutputStream 案例:将小写转化为大写 /* * 内存操作流,将大写字母转化为小写字母(ByteArrayInp ...
- 【Android开发精要笔记】Android组件模型解析
Android组件模型解析 Android中的Mashup 将应用切分成不同类别的组件,通过统一的定位模型和接口标准将他们整合在一起,来共同完成某项任务.在Android的Mashup模式下,每个组件 ...
- Understanding sun.misc.Unsafe
转自: https://dzone.com/articles/understanding-sunmiscunsafe The biggest competitor to the Java virtua ...
- 多线程的那点儿事(之windows锁)
在windows系统中,系统本身为我们提供了很多锁.通过这些锁的使用,一方面可以加强我们对锁的认识,另外一方面可以提高代码的性能和健壮性.常用的锁以下四种:临界区,互斥量,信号量,event. (1) ...