-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1482.cpp
More file actions
executable file
·46 lines (40 loc) · 811 Bytes
/
LC1482.cpp
File metadata and controls
executable file
·46 lines (40 loc) · 811 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
*/
class Solution {
public:
int minDays(vector<int>& bloomDay, int m, int k) {
int low, mid, high, n = bloomDay.size();
if ((long long) m * k > n)
return -1;
// helper function
auto good = [&](int day) -> bool {
int len = 0, need = m;
for (int& x: bloomDay) {
if (x <= day)
len++;
else
len = 0;
if (len == k) {
len = 0;
need--;
}
if (need == 0)
return true;
}
return false;
};
auto it = minmax_element(bloomDay.begin(), bloomDay.end());
low = *it.first;
high = *it.second;
// binary search
while (low < high) {
mid = (low + high) / 2;
if (good(mid))
high = mid;
else
low = mid + 1;
}
return high;
}
};