Minimum Number of Days to Make m Bouquets - Problem
You're managing a flower garden where you need to create beautiful bouquets for a special event! ๐ธ Given an integer array
Important constraints:
โข Each bouquet must use exactly
โข Each flower can only be used in one bouquet
โข If it's impossible to make
For example, if
bloomDay where bloomDay[i] represents the day when the i-th flower will bloom, and integers m (number of bouquets needed) and k (flowers per bouquet), your task is to find the minimum number of days you need to wait to make exactly m bouquets.Important constraints:
โข Each bouquet must use exactly
k adjacent flowersโข Each flower can only be used in one bouquet
โข If it's impossible to make
m bouquets, return -1For example, if
bloomDay = [1,10,3,10,2], m = 3, and k = 1, you need 3 bouquets with 1 flower each. On day 2, flowers at positions 0, 2, and 4 have bloomed, giving you exactly 3 bouquets! Input & Output
example_1.py โ Basic Case
$
Input:
bloomDay = [1,10,3,10,2], m = 3, k = 1
โบ
Output:
3
๐ก Note:
We need 3 bouquets with 1 flower each. By day 3, flowers at positions 0 (bloomed day 1), 2 (bloomed day 3), and 4 (bloomed day 2) are available, giving us exactly 3 bouquets.
example_2.py โ Adjacent Flowers Required
$
Input:
bloomDay = [1,10,3,10,2], m = 3, k = 2
โบ
Output:
-1
๐ก Note:
We need 3 bouquets with 2 adjacent flowers each (total 6 flowers), but we only have 5 flowers in the garden. It's impossible to make 3 bouquets.
example_3.py โ Longer Wait Time
$
Input:
bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
โบ
Output:
12
๐ก Note:
We need 2 bouquets with 3 adjacent flowers each. By day 7, positions 0-2 and 3-5 can form bouquets, but position 4 blooms on day 12. So we need to wait until day 12.
Visualization
Tap to expand
Understanding the Visualization
1
Initial Garden
All flowers are buds, waiting to bloom according to their schedule
2
Binary Search Range
We know the answer is between day 1 and the latest bloom day
3
Check Middle Day
For each candidate day, count how many complete bouquets we can make
4
Narrow Search
If we can make enough bouquets, try an earlier day. Otherwise, try a later day
Key Takeaway
๐ฏ Key Insight: Binary search works because if we can make enough bouquets on day X, we can definitely make them on any day > X (monotonic property)!
Time & Space Complexity
Time Complexity
O(n ร log(max(bloomDay)))
Binary search takes log(max(bloomDay)) iterations, and each iteration requires O(n) to check if bouquets can be made
โก Linearithmic
Space Complexity
O(1)
Only using constant extra space for binary search variables
โ Linear Space
Constraints
- 1 โค bloomDay.length โค 105
- 1 โค bloomDay[i] โค 109
- 1 โค m, k โค bloomDay.length
- Important: Each bouquet needs exactly k adjacent flowers
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code