[LeetCode] Minimum Number of Days to Make m Bouquets

1482. Minimum Number of Days to Make m Bouquets

You are given an integer array bloomDay, an integer m and an integer k.

You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.

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
class Solution {
int search(vector<int>& A, int d, int k) {
int l = 0, r = 0, sum = 0, n = A.size();

while(r < n) {
if(A[r] > d) {
l = ++r;
} else {
++r;
}
if(r - l == k) {
sum++;
l = r;
}
}


return sum;
}
public:
int minDays(vector<int>& bloomDay, int m, int k) {
if(1ll * m * k > bloomDay.size()) return -1;
int l = 1, r = INT_MAX;
while(l <= r) {
int d = l + (r-l) / 2;
int s = search(bloomDay, d, k);
if(s >= m) r = d - 1;
else l = d + 1;
}
return l;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/30/PS/LeetCode/minimum-number-of-days-to-make-m-bouquets/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.