This problem is about figuring out the minimum number of days required to make m
bouquets, where each bouquet is formed from k
adjacent flowers that have bloomed.
Understanding the Setup
We are given a list of bloom days for each flower. A flower can only be used to form a bouquet on or after the day it blooms. For every bouquet, we need exactly k
adjacent flowers. If they're not next to each other, we cannot use them in the same bouquet.
How to Think About It
Imagine checking a particular day — let's say day 6. You can only use flowers that bloom on or before day 6. Now go through the array, and every time you see k
or more adjacent bloomed flowers, you make one bouquet. Count how many bouquets you can make this way. If you can make at least m
bouquets, then day 6 is a valid answer.
Our goal is to find the minimum such day. So, instead of checking each day one by one (which would be slow), we use binary search between the smallest and largest bloom days. At each step, we check if it’s possible to make enough bouquets by that day. If yes, we try an earlier day (move left); if not, we try a later day (move right).
Special Cases to Consider
- Not Enough Flowers: If
m × k
is greater than the total number of flowers, it's impossible to make that many bouquets. We return -1
.
- All Flowers Bloom Late: If flowers bloom very late (e.g., all values are huge), the answer may be a large number, but binary search helps us get there quickly.
- Empty Array: If the input array is empty, no bouquets can be made. We return
-1
.
- Edge Size Matching: If the total number of flowers is exactly
m × k
, then we must use every flower and they must be positioned in a way that allows adjacency.
This approach gives us a clean and efficient solution with time complexity O(n × log(max bloom day)), where n
is the number of flowers.