-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1482.java
More file actions
46 lines (37 loc) · 1.03 KB
/
1482.java
File metadata and controls
46 lines (37 loc) · 1.03 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
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
if ((long)m * k > bloomDay.length) return -1;
int low = Integer.MAX_VALUE;
int high = Integer.MIN_VALUE;
for (int val : bloomDay) {
low = Math.min(low, val);
high = Math.max(high, val);
}
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (canMake(bloomDay, m, k, mid)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
boolean canMake(int[] bloomDay, int m, int k, int day) {
int count = 0, flowers = 0;
for (int val : bloomDay) {
if (val <= day) {
flowers++;
if (flowers == k) {
count++;
flowers = 0;
}
} else {
flowers = 0;
}
}
return count >= m;
}
}