Untitled

 avatar
unknown
plain_text
a year ago
775 B
3
Indexable
class Solution {
public:
    bool possible(int cap,vector<int>&weights,int days){
        int count=1,sum=0;
        for(int i=0;i<weights.size();i++){
            if(sum+weights[i]>cap){
                count++;
                sum=0;
            }
            sum+=weights[i];
        }
        if(count>days) return false;
        else return true;
    }

    int shipWithinDays(vector<int>& weights, int days) {
        
        int s=INT_MIN,e,mid;
        for(auto i:weights){
            s=max(s,i);
            e+=i;
        }

        while(s<=e){
            mid=s+(e-s)/2;
            if(possible(mid,weights,days)){//possible better the answer
                e=mid-1;
            }else{
                s=mid+1;
            }
        }
        return s;
    }
};
Editor is loading...
Leave a Comment