Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 c=0,rd=1;
- for(auto weight:weights)
- {
- if(c+weight>mid)
- {
- c=0;
- rd++;
- }
- c+=weight;
- }
- if(rd<=days)
- {
- r = mid-1;
- }
- else
- {
- l = mid+1;
- }
- }
- return l;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment