Advertisement
nikunjsoni

1482-1

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