Minimum Number of Days to Make m Bouquets LeetCode solution in JavaScript

1
var minDays = function (bloomDay, m, k) {
2
if (m * k > bloomDay.length) {
3
return -1;
4
}
5

6
let left = 0;
7
let right = 0;
8

9
for (const day of bloomDay) {
10
left = Math.min(day, left);
11
right = Math.max(day, right);
12
}
13

14
let ans = right;
15

16
while (left < right) {
17
const day = Math.floor((left + right) / 2);
18

19
let count = 0;
20
let current = 0;
21

22
for (let j = 0; j < bloomDay.length; j++) {
23
if (bloomDay[j] <= day) {
24
current++;
25
} else {
26
current = 0;
27
}
28

29
if (current === k) {
30
count++;
31
current = 0;
32
}
33
}
34

35
if (count === m) {
36
ans = Math.min(ans, day);
37
}
38

39
if (count < m) {
40
left = day + 1;
41
} else {
42
right = day;
43
}
44
}
45

46
return ans;
47
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0