-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDaysForBouquets.cpp
More file actions
50 lines (48 loc) · 1.58 KB
/
Copy pathMinimumDaysForBouquets.cpp
File metadata and controls
50 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
int findMax(vector<int>& bloomDay) {
int maxi = INT_MIN;
int n = bloomDay.size();
for (int i = 0;i<n;i++) {
maxi = max(maxi,bloomDay[i]);
}
return maxi;
}
bool canMake(vector<int>& bloomDay,int dayLimit,int m, int k) {
int cntFlowers =0;
int totalBouquets = 0;
for (int i=0;i<bloomDay.size(); i++) {
if(bloomDay[i]<=dayLimit) {
cntFlowers++;
if(cntFlowers == k) {
totalBouquets++;
cntFlowers=0;
if(totalBouquets == m) {
return true;
}
}
}else {
cntFlowers =0;
}
}
return false;
}
int minDays(vector<int>& bloomDay,int m, int k) {
if((long long)m*k>bloomDay.size()) {
return -1;
}
int low = 1;
int high = findMax(bloomDay);
int ans = -1;
while (low<=high) {
int mid = low +(high-low)/2;
if(canMake(bloomDay,mid,m,k)) {
ans = mid;
high = mid-1;
}else {
low = mid+1;
}
}
return ans;
}
};