Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minDays(vector<int>& bloomDay, int m, int k) {
- // If not possible to make m bouquets.
- if(m*k > bloomDay.size())
- return -1;
- int l=0, r=(1e9)+1;
- // Binary search the leftmost val.
- while(l<r){
- int mid = (l+r)/2;
- if(!canMake(bloomDay, m, k, mid))
- l = mid+1;
- else
- r = mid;
- }
- return l;
- }
- bool canMake(vector<int>& bloomDay, int m, int k, int day){
- int count=0, made=0;
- // Check if m bouquets can be made.
- for(int i=0; i<bloomDay.size(); i++){
- if(bloomDay[i] <= day)
- count++;
- else
- count = 0;
- if(count >= k){
- made++;
- count = 0;
- }
- }
- if(made >= m) return true;
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement