-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path1482
42 lines (37 loc) · 968 Bytes
/
1482
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
class Solution {
public:
int minDays(vector<int>& arr, int m, int k) {
int i = 0;
int j = *max_element(arr.begin(), arr.end());
int answer = -1;
while (i <= j) {
int mid = (i + j) / 2;
vector<int> vis(arr.size(), 0);
for (int i = 0; i < arr.size(); i++) {
if (arr[i] <= mid) {
vis[i] = 1;
}
}
int c = 0;
int m1 = 0;
for (int i = 0; i < vis.size(); i++) {
if (vis[i] == 1) {
c++;
} else {
c = 0;
}
if (c == k) {
m1++;
c = 0; // Reset the count after forming a bouquet
}
}
if (m1 >= m) {
answer = mid;
j = mid - 1; // Try to find a smaller valid answer
} else {
i = mid + 1;
}
}
return answer;
}
};