leetcode1011. 在 D 天内送达包裹的能力

class Solution {
public:
    int shipWithinDays(vector<int>& weights, int days) {
        int l=*max_element(weights.begin(),weights.end());
        int r=accumulate(weights.begin(),weights.end(),0);
        while(l<r){
            int mid=(l+r)/2;
            int need=1;
            int cur=0;
            for(int w:weights){
                if(cur+w>mid){
                    need++;
                    cur=0;
                }
                cur+=w;
            }
            if(need<=days)r=mid;
            else l=mid+1;
        }
        return l;
    }
};