-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaysToMakeMBouquets.java
More file actions
46 lines (37 loc) · 1.07 KB
/
DaysToMakeMBouquets.java
File metadata and controls
46 lines (37 loc) · 1.07 KB
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
package Searching;
public class DaysToMakeMBouquets {
public static int minDays(int[] bloomDay, int m, int k) {
if ((long) m * k > bloomDay.length)
return -1;
int right = -1, left = 1;
for (int day : bloomDay) {
right = Math.max(day, right);
}
int ans = right;
while (left <= right) {
int mid = left + (right - left) / 2;
if (isPossible(bloomDay, m, k, mid)) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
public static boolean isPossible(int[] bloom, int m, int k, int day) {
int count = 0;
for (int i = 0; i < bloom.length; ) {
int temp = k;
int j = i;
while (j < bloom.length && temp > 0 && bloom[j] <= day) {
j++;
temp--;
}
if (temp == 0) count++;
if (temp < k) i = j;
else i = j + 1;
}
return count >= m;
}
}