-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1482-minimum-number-of-days-to-make-m-bouquets.js
59 lines (54 loc) · 1.4 KB
/
1482-minimum-number-of-days-to-make-m-bouquets.js
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
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 1482. Minimum Number of Days to Make m Bouquets
* https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
* Difficulty: Medium
*
* You are given an integer array bloomDay, an integer m and an integer k.
*
* You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the
* garden.
*
* The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be
* used in exactly one bouquet.
*
* Return the minimum number of days you need to wait to be able to make m bouquets from the garden.
* If it is impossible to make m bouquets return -1.
*/
/**
* @param {number[]} bloomDay
* @param {number} m
* @param {number} k
* @return {number}
*/
var minDays = function(bloomDay, m, k) {
if (m * k > bloomDay.length) return -1;
let left = 1;
let right = Math.max(...bloomDay);
let result = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (canMakeBouquets(mid)) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
function canMakeBouquets(days) {
let bouquets = 0;
let flowers = 0;
for (const bloom of bloomDay) {
if (bloom <= days) {
flowers++;
if (flowers === k) {
bouquets++;
flowers = 0;
}
} else {
flowers = 0;
}
}
return bouquets >= m;
}
};