Advertisement
nikunjsoni

1482

May 21st, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minDays(vector<int>& bloomDay, int m, int k) {
  4.         vector<int> bdays(bloomDay);
  5.         // If not possible to make m bouquets.
  6.         if(m*k > bloomDay.size())
  7.             return -1;
  8.        
  9.         int l=0, r=(1e9)+1;
  10.         // Binary search the leftmost val.
  11.         while(l<r){
  12.             int mid = (l+r)/2;
  13.             if(!canMake(bdays, m, k, mid))
  14.                 l = mid+1;
  15.             else
  16.                 r = mid;
  17.         }
  18.         return l;
  19.     }
  20.    
  21.     bool canMake(vector<int>& bloomDay, int m, int k, int day){
  22.         int count=0, made=0;
  23.         // Check if m bouquets can be made.
  24.         for(int i=0; i<bloomDay.size(); i++){
  25.             if(bloomDay[i] <= day)
  26.                 count++;
  27.             else
  28.                 count = 0;
  29.             if(count >= k){
  30.                 made++;
  31.                 count = 0;
  32.             }
  33.         }
  34.         if(made >= m) return true;
  35.         return false;
  36.     }
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement