-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19jun.txt
42 lines (39 loc) · 1.07 KB
/
19jun.txt
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(int[] bloomDay, int m, int k) {
if (bloomDay.length < m * k) {
return -1;
}
int low = 1, high = 1, n = bloomDay.length, ans = -1;
if (m > (n / k)) {
return -1;
}
for (int i = 0; i < bloomDay.length; i++) {
high = Math.max(high, bloomDay[i]);
}
while (low <= high) {
int mid = low + (high - low) / 2;
if (canHappen(mid, bloomDay, m, k)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
private boolean canHappen(int days, int[] bloomDay, int m, int k) {
int K = 0, bouquets = 0;
for (int i = 0; i < bloomDay.length; i++) {
if (bloomDay[i] <= days) {
K++;
if (K == k) {
bouquets++;
K = 0;
}
} else {
K = 0;
}
}
return bouquets >= m;
}
};