A conveyor belt has packages that must be shipped from one port to another within days days.
The ith 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 days days.
题意 :将一个数组最多分为days份(连续),求每组和 最大值的最小值。

思路:贪心+二分答案
假设当船的运载能力为x时,可以在days天内运送完所有包裹,那么只要运载能力大于x,同样可以在days天内运送完所有包裹。
结论:
存在一共运载能力的【下限】x0,当x>=x0时,可以在days天内运送完所有包裹,当x<x0时,无法在days天内运送完所有包裹。
所以,x0就是需要求出的答案。
可以使用二分查找的方法找出x0。
判定问题:给定运载能力x,能否在days天内运送完所有的包裹?
通过贪心的方法解决:
由于必须按照数组Weights中包裹的顺序进行运送,因此我们从数组weights的首元素开始遍历,将连续的包裹都安排在同一天进行运送。当这批包裹的重量大于运载能力x时,就需要将最后一共包裹拿出来,安排在新的一天,并继续往下遍历。当我们遍历完整个数组后,就得到了最少需要运输的天数。
将最少需要运送的天数与days比较,就可以解决这个问题。
注意check的逻辑:
class Solution {
public int shipWithinDays(int[] weights, int days) {
int l=1,r=500*50000;
while(l<r){
int mid=(l+r)/2;
int need=1,cur=0;
for(int w:weights){
if(w>mid)need=(int)1e6;
if(cur+w>mid){
need++;
cur=0;
}
cur+=w;
}
if(need<=days){
r=mid;
}else l=mid+1;
}
return l;
}
}