Minimum Number of Days to Make m Bouquets
Minimum Number of Days to Make m Bouquets is LeetCode 1482 (Medium). Flower i blooms on day bloomDay[i]. A bouquet takes k adjacent bloomed flowers, and each flower can go into only one bouquet. Return the fewest days to wait before m bouquets can be made, or -1 if that is never possible.
- Days reach 10⁹, so trying every day one by one is far too slow.
m × kcan reach 10¹¹, which does not fit in a 32-bit integer.
- bloomDay.length == n
- 1 <= n <= 10⁵
- 1 <= bloomDay[i] <= 10⁹
- 1 <= m <= 10⁶
- 1 <= k <= n
Intuition
Ask a yes-or-no question instead: can m bouquets be made by day d? One left-to-right pass answers it. Count bloomed flowers in a row; when the run reaches k, that is a bouquet and the run starts again from 0; an unbloomed flower resets the run.
The answer to that question only ever turns from no to yes as d grows, because waiting longer never un-blooms a flower. So the days split into a block of "no" followed by a block of "yes", and binary search finds the first yes. It lies between the earliest and the latest bloom day: before the earliest nothing has bloomed, and by the latest everything has.
"Find the smallest value that makes something possible", where checking one value is easy and a larger value never hurts, is the binary search on answer pattern. Koko Eating Bananas and Capacity to Ship Packages Within D Days follow the same pattern.
Approach
Before reading on, write a function that says whether m bouquets are possible on a given day. Then check that a later day can never turn a yes into a no, and decide the lowest and highest day worth searching. Aim for O(n log D), where D is the largest bloom day.
Rule out the impossible case first
If m * k > n, there are not enough flowers even when all of them bloom, so return -1. In C++ and Java multiply as a 64-bit long, because m * k can reach 10¹¹.
Check one day in a single pass
Walk through bloomDay. A flower with bloomDay[i] <= day adds 1 to run; when run hits k, add a bouquet and set run back to 0 so those flowers are not reused. A flower that has not bloomed sets run to 0, since a bouquet cannot skip over it.
Search between the earliest and latest bloom day
Set lo = min(bloomDay) and hi = max(bloomDay). Once the impossible case is ruled out, hi always works, because every flower has bloomed by then and m * k <= n.
Keep the first day that works
While lo < hi, test mid = (lo + hi) // 2. If it works, keep it as a candidate with hi = mid; if not, every earlier day fails too, so set lo = mid + 1. When the two meet, lo is the smallest working day.
Code in Python, C++ and Java
Common pitfalls
Counting bloomed flowers without checking they are adjacent
bloomed = sum(1 for d in bloomDay if d <= day) return bloomed // k >= m
run = 0 when a flower has not bloomed; bouquet only when run reaches k
Flowers on both sides of an unbloomed one cannot share a bouquet. For bloomDay = [1, 10, 1], m = 1, k = 2, two flowers are open on day 1, so this answers 1, but the two open flowers are not neighbours and the real answer is 10.
Multiplying m and k as 32-bit integers
if (m * k > bloomDay.size()) return -1;
if ((long long)m * k > (long long)bloomDay.size()) return -1;
With m = 10⁶ and k = 10⁵ the product is 10¹¹, which wraps around in an int and can come out small or negative. The check then passes and the search returns a day instead of -1.
Edge cases
Every flower must be used, so the answer is the last bloom day, max(bloomDay). The search ends there because no earlier day has all flowers open.
Adjacency stops mattering: each bloomed flower is its own bouquet, so the answer is the m-th smallest bloom day. The same code finds it.
Complexity
Minimum Number of Days to Make m Bouquets FAQ
Why can't I just try every day from 1 upward?
It gives the right answer, but days go up to 10⁹, so up to 10⁹ passes over 10⁵ flowers. Trying only the distinct bloom days in sorted order caps it at n passes, which is still O(n²) in the worst case. Binary search needs about 30 passes.
Does the answer always equal one of the bloom days?
Yes. Nothing changes between two bloom days, so if a day works, the latest bloom day at or before it works too. You can binary search over the sorted distinct bloom days instead of every integer; it takes the same number of rounds but makes this fact visible.
Why reset the run to 0 after a bouquet instead of letting it keep counting?
Each flower can be in one bouquet only, and the reset is what splits a long stretch into separate bouquets. For four open flowers in a row with k = 2, resetting gives 2 bouquets. Without it the run goes 1, 2, 3, 4, matches k only once, and counts just 1.