-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMin number of days to make bouquets.
More file actions
62 lines (48 loc) · 1.47 KB
/
Copy pathMin number of days to make bouquets.
File metadata and controls
62 lines (48 loc) · 1.47 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
51
52
53
54
55
56
57
58
59
60
61
62
https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/submissions/
class Solution {
bool possible(vector<int>& bloomDay, int m, int k, int mid){
/* i can make a boquet , only if k adjacent flowers are there to bloom*/
int adj_flowers= 0; /* number of adj flowers*/
int k_days = 0;
int n_bqt = 0;
for(int i = 0; i < bloomDay.size(); i++){
if(bloomDay[i] <= mid){
adj_flowers++;
}
else{
adj_flowers = 0;
}
/**/
if(adj_flowers == k){
adj_flowers = 0;
n_bqt++;
}
if(n_bqt == m){
return true;
}
}
return false;
}
public:
int minDays(vector<int>& bloomDay, int m, int k) {
// sort(bloomDay.begin(), bloomDay.end());
int low = INT_MAX;
int high = INT_MIN;
for (int i = 0; i < bloomDay.size(); i++) {
low = min(low, bloomDay[i]);
high = max(high, bloomDay[i]);
}
int ans = -1;
while(low <= high){
int mid = low + (high-low)/2;
if(possible(bloomDay, m, k, mid)){
ans = mid;
high = mid -1;
}
else{
low = mid + 1;
}
}
return ans;
}
};