Minimum Number of Days to Make m Bouquets
Minimum Number of Days to Make m Bouquets is a medium binary search problem solved with the binary search on answer space pattern.
The best approach, optimal (binary search on the day), runs in O(n log D) time and O(1) space.
Below are 2 approaches in Java, from brute force (try each distinct day) up.
Problem
Flower i blooms on day bloomDay[i]. A bouquet needs k adjacent bloomed flowers, and each flower can be used once. Return the minimum number of days to wait before you can make m bouquets, or -1 if it is impossible.
Examples
Example 1
- Input
bloomDay = [1, 10, 3, 10, 2], m = 3, k = 1- Output
3- Why
- By day 3 flowers 0, 2 and 4 have bloomed: three bouquets of 1.
Example 2
- Input
bloomDay = [7, 7, 7, 7, 12, 7, 7], m = 2, k = 3- Output
12- Why
- On day 7 only one run of 3 adjacent flowers exists; on day 12 there are two.
Constraints
1 <= n <= 10^5,1 <= bloomDay[i] <= 10^91 <= m <= 10^6,1 <= k <= n- Flowers in a bouquet must be adjacent.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.
| Approach | Time | Space |
|---|---|---|
| Brute force (try each distinct day) | O(n²) | O(n) |
| Optimal (binary search on the day) | O(n log D) | O(1) |
1Brute force (try each distinct day)
O(n²)O(n)Sort the distinct bloom days and return the first day on which m bouquets can be made.
- For each candidate day in increasing order, count bouquets greedily.
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
if ((long) m * k > bloomDay.length) return -1;
int[] days = Arrays.stream(bloomDay).distinct().sorted().toArray();
for (int d : days) if (bouquets(bloomDay, d, k) >= m) return d;
return -1;
}
private int bouquets(int[] b, int day, int k) {
int count = 0, run = 0;
for (int x : b) {
run = x <= day ? run + 1 : 0;
if (run == k) { count++; run = 0; }
}
return count;
}
}2Optimal (binary search on the day)
O(n log D)D is the range of bloom days; each check is O(n).O(1)The answer lies between min(bloomDay) and max(bloomDay), and feasibility is monotonic. Binary search for the first day where a greedy scan of adjacent bloomed runs yields at least m bouquets.
- If m · k > n (as long), return -1.
- lo = min, hi = max; if bouquets(mid) >= m then hi = mid, else lo = mid + 1.
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
if ((long) m * k > bloomDay.length) return -1;
int lo = Integer.MAX_VALUE, hi = 0;
for (int b : bloomDay) { lo = Math.min(lo, b); hi = Math.max(hi, b); }
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (bouquets(bloomDay, mid, k) >= m) hi = mid; else lo = mid + 1;
}
return lo;
}
private int bouquets(int[] b, int day, int k) {
int count = 0, run = 0;
for (int x : b) {
run = x <= day ? run + 1 : 0;
if (run == k) { count++; run = 0; }
}
return count;
}
}Edge cases to test
- m · k > n (impossible, return -1; the product can overflow int)
- Bouquets split by one late flower
Hints
Hint 1
If you can make m bouquets by day d, you can also by any later day.
FAQ
What is the best time complexity for Minimum Number of Days to Make m Bouquets?
Optimal (binary search on the day) runs in O(n log D) time and O(1) extra space. D is the range of bloom days; each check is O(n).
Which pattern does Minimum Number of Days to Make m Bouquets use?
It is a binary search problem that uses the binary search on answer space pattern. Other problems with the same pattern: Koko Eating Bananas.
Is there a brute force solution for Minimum Number of Days to Make m Bouquets?
Yes. Brute force (try each distinct day) takes O(n²) time and O(n) space. Sort the distinct bloom days and return the first day on which m bouquets can be made.
Which edge cases should I test for Minimum Number of Days to Make m Bouquets?
m · k n (impossible, return -1; the product can overflow int); Bouquets split by one late flower.