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;
    }

};

 

posted @ 2020-11-11 16:36  阿破  阅读(94)  评论(0编辑  收藏  举报