Skip to content

Commit 2fcf127

Browse files
committed
Add solution #1482
1 parent 4a42a2c commit 2fcf127

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,323 LeetCode solutions in JavaScript
1+
# 1,324 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1133,6 +1133,7 @@
11331133
1478|[Allocate Mailboxes](./solutions/1478-allocate-mailboxes.js)|Hard|
11341134
1480|[Running Sum of 1d Array](./solutions/1480-running-sum-of-1d-array.js)|Easy|
11351135
1481|[Least Number of Unique Integers after K Removals](./solutions/1481-least-number-of-unique-integers-after-k-removals.js)|Medium|
1136+
1482|[Minimum Number of Days to Make m Bouquets](./solutions/1482-minimum-number-of-days-to-make-m-bouquets.js)|Medium|
11361137
1486|[XOR Operation in an Array](./solutions/1486-xor-operation-in-an-array.js)|Easy|
11371138
1491|[Average Salary Excluding the Minimum and Maximum Salary](./solutions/1491-average-salary-excluding-the-minimum-and-maximum-salary.js)|Easy|
11381139
1492|[The kth Factor of n](./solutions/1492-the-kth-factor-of-n.js)|Medium|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* 1482. Minimum Number of Days to Make m Bouquets
3+
* https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array bloomDay, an integer m and an integer k.
7+
*
8+
* You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the
9+
* garden.
10+
*
11+
* The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be
12+
* used in exactly one bouquet.
13+
*
14+
* Return the minimum number of days you need to wait to be able to make m bouquets from the garden.
15+
* If it is impossible to make m bouquets return -1.
16+
*/
17+
18+
/**
19+
* @param {number[]} bloomDay
20+
* @param {number} m
21+
* @param {number} k
22+
* @return {number}
23+
*/
24+
var minDays = function(bloomDay, m, k) {
25+
if (m * k > bloomDay.length) return -1;
26+
27+
let left = 1;
28+
let right = Math.max(...bloomDay);
29+
let result = -1;
30+
31+
while (left <= right) {
32+
const mid = Math.floor((left + right) / 2);
33+
if (canMakeBouquets(mid)) {
34+
result = mid;
35+
right = mid - 1;
36+
} else {
37+
left = mid + 1;
38+
}
39+
}
40+
41+
return result;
42+
43+
function canMakeBouquets(days) {
44+
let bouquets = 0;
45+
let flowers = 0;
46+
for (const bloom of bloomDay) {
47+
if (bloom <= days) {
48+
flowers++;
49+
if (flowers === k) {
50+
bouquets++;
51+
flowers = 0;
52+
}
53+
} else {
54+
flowers = 0;
55+
}
56+
}
57+
return bouquets >= m;
58+
}
59+
};

0 commit comments

Comments
 (0)