|
| 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 | + |
| 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