Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b4f7865

Browse files
Merge pull request #47 from codewithhridoy/javascript
medium: 1482. Minimum Number of Days to Make m Bouquets
2 parents 3b4ec0a + 170d8f4 commit b4f7865

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {number[]} bloomDay
3+
* @param {number} m
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
function minDays(bloomDay, m, k) {
8+
// Helper function to check if we can make m bouquets in given days
9+
const canMakeBouquets = (days) => {
10+
let bouquets = 0,
11+
flowers = 0;
12+
for (let i = 0; i < bloomDay.length; i++) {
13+
if (bloomDay[i] <= days) {
14+
flowers++;
15+
if (flowers === k) {
16+
bouquets++;
17+
flowers = 0; // reset flowers count for the next bouquet
18+
}
19+
} else {
20+
flowers = 0; // reset flowers count if the current flower hasn't bloomed
21+
}
22+
}
23+
return bouquets >= m;
24+
};
25+
26+
// Edge case: if total flowers are less than required flowers for m bouquets
27+
if (bloomDay.length < m * k) {
28+
return -1;
29+
}
30+
31+
// Initialize binary search bounds
32+
let low = Math.min(...bloomDay);
33+
let high = Math.max(...bloomDay);
34+
let result = -1;
35+
36+
// Perform binary search
37+
while (low <= high) {
38+
const mid = Math.floor((low + high) / 2);
39+
if (canMakeBouquets(mid)) {
40+
result = mid;
41+
high = mid - 1; // try for a smaller number of days
42+
} else {
43+
low = mid + 1; // try for a larger number of days
44+
}
45+
}
46+
47+
return result;
48+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [1482. Minimum Number of Days to Make m Bouquets](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets)
2+
3+
---
4+
5+
title: "Minimum Number of Days to Make m Bouquets"
6+
summary: "This post explains how to determine the minimum number of days required to make a given number of bouquets using a binary search approach."
7+
date: "2024年06月19日"
8+
modifiedDate: "2024年06月19日"
9+
tags: ["algorithm", "binary search", "JavaScript"]
10+
slug: "minimum-number-of-days-bouquets"
11+
12+
---
13+
14+
![image.png](https://assets.leetcode.com/users/images/1b8473bf-9508-41c5-aaac-168d6f74ccab_1718768220.4949567.png)
15+
16+
# Minimum Number of Days to Make m Bouquets
17+
18+
This post explains how to determine the minimum number of days required to make a given number of bouquets using a binary search approach.
19+
20+
## Intuition
21+
22+
To solve this problem, the key is to realize that we can use a binary search to find the minimum number of days required. By defining a helper function that checks if we can make the required number of bouquets within a given number of days, we can iteratively narrow down our search range until we find the optimal solution.
23+
24+
## Approach
25+
26+
1. **Helper Function**: Create a function `canMakeBouquets` to determine if we can make the required number of bouquets within a given number of days.
27+
2. **Edge Case Handling**: If the total number of flowers is less than required, return `-1`.
28+
3. **Binary Search**: Use binary search to find the minimum number of days:
29+
- Initialize the search bounds (`low` and `high`) based on the minimum and maximum values in the `bloomDay` array.
30+
- Adjust the bounds based on whether the current midpoint allows making the required bouquets.
31+
- Continue the search until the optimal number of days is found.
32+
33+
## Complexity
34+
35+
- **Time Complexity**: $$O(n \log D)$$ where \(n\) is the number of flowers and \(D\) is the range of days between the minimum and maximum bloom days.
36+
- **Space Complexity**: $$O(1)$$ since we are using a constant amount of extra space.
37+
38+
## Code
39+
40+
```javascript
41+
/**
42+
* @param {number[]} bloomDay
43+
* @param {number} m
44+
* @param {number} k
45+
* @return {number}
46+
*/
47+
function minDays(bloomDay, m, k) {
48+
// Helper function to check if we can make m bouquets in given days
49+
const canMakeBouquets = (days) => {
50+
let bouquets = 0,
51+
flowers = 0;
52+
for (let i = 0; i < bloomDay.length; i++) {
53+
if (bloomDay[i] <= days) {
54+
flowers++;
55+
if (flowers === k) {
56+
bouquets++;
57+
flowers = 0; // reset flowers count for the next bouquet
58+
}
59+
} else {
60+
flowers = 0; // reset flowers count if the current flower hasn't bloomed
61+
}
62+
}
63+
return bouquets >= m;
64+
};
65+
66+
// Edge case: if total flowers are less than required flowers for m bouquets
67+
if (bloomDay.length < m * k) {
68+
return -1;
69+
}
70+
71+
// Initialize binary search bounds
72+
let low = Math.min(...bloomDay);
73+
let high = Math.max(...bloomDay);
74+
let result = -1;
75+
76+
// Perform binary search
77+
while (low <= high) {
78+
const mid = Math.floor((low + high) / 2);
79+
if (canMakeBouquets(mid)) {
80+
result = mid;
81+
high = mid - 1; // try for a smaller number of days
82+
} else {
83+
low = mid + 1; // try for a larger number of days
84+
}
85+
}
86+
87+
return result;
88+
}
89+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /