30.数组分段使最大值最小

class Solution {
public:
    int get_buckets(vector<int> &nums,int volume){
        int buckets = 1;
        int ans = 0;
        for (auto num:nums){
            ans += num;
            if(ans > volume){
                buckets += 1;
                ans = num;
            }
        }
        return buckets;
    }
    int shipWithinDays(vector<int>& nums, int D) {
        int low = *max_element(nums.begin(),nums.end());
        int high = accumulate(nums.begin(),nums.end(),0);
        while(low < high){
            int mid = low+(high-low)/2;
            int buckets = get_buckets(nums,mid);
            if (buckets <= D)high = mid;
            else low = mid +1;
        }

        return low;
    }

};
原文地址:https://www.cnblogs.com/apo2019/p/13959401.html