-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
46 lines (38 loc) · 1.11 KB
/
main.java
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
class Solution {
// Return the number of maximum bouquets that can be made on day mid.
private int getNumOfBouquets(int[] bloomDay, int mid, int k) {
int numOfBouquets = 0;
int count = 0;
for (int i = 0; i < bloomDay.length; i++) {
// If the flower is bloomed, add to the set. Else reset the count.
if (bloomDay[i] <= mid) {
count++;
} else {
count = 0;
}
if (count == k) {
numOfBouquets++;
count = 0;
}
}
return numOfBouquets;
}
public int minDays(int[] bloomDay, int m, int k) {
int start = 0;
int end = 0;
for (int day : bloomDay) {
end = Math.max(end, day);
}
int minDays = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (getNumOfBouquets(bloomDay, mid, k) >= m) {
minDays = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return minDays;
}
}