-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLc1482.java
47 lines (45 loc) · 1.08 KB
/
Lc1482.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
47
package leetcode;
/**
* @author Kuma
* @date 2021年5月16日
* 1482.制作 m 束花所需的最少天数
*/
public class Lc1482 {
public int minDays(int[] bloomDay, int m, int k) {
if (bloomDay.length < m * k){
return -1;
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i : bloomDay){
min = Math.min(i, min);
max = Math.max(i, max);
}
int l = min;
int r = max;
while (l < r){
int mid = l + r >> 1;
if (check(bloomDay, m, k, mid)){
r = mid;
}else {
l = mid + 1;
}
}
return check(bloomDay, m, k, r) ? r : -1;
}
boolean check(int[] bloomDay, int m, int k, int mid){
int num = 0;
for (int i : bloomDay){
if (i <= mid){
num++;
}else if (num > 0){
num = 0;
}
if (num == k){
m--;
num = 0;
}
}
return m <= 0;
}
}