jayati

Capacity To Ship Packages Within D Days

Feb 22nd, 2023
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int shipWithinDays(vector<int>& weights, int days) {
  4.         int l = *max_element(weights.begin(),weights.end());
  5.         int r = accumulate(weights.begin(),weights.end(),0);
  6.         while(l<=r)
  7.         {
  8.             int mid=(l+r)/2;
  9.             int c=0,rd=1;
  10.             for(auto weight:weights)
  11.             {
  12.                if(c+weight>mid)
  13.                {
  14.                    c=0;
  15.                    rd++;
  16.                }
  17.                c+=weight;
  18.             }
  19.             if(rd<=days)
  20.             {
  21.                 r = mid-1;
  22.             }
  23.             else
  24.             {
  25.                 l = mid+1;
  26.             }
  27.  
  28.         }
  29.         return l;
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment