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

For 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
๐ŸŒธ Bouquet Garden Problem VisualizationExample: bloomDay = [1, 10, 3, 10, 2], m = 3, k = 1Garden Layout (position โ†’ bloom day):1Pos 010Pos 13Pos 210Pos 32Pos 4Binary Search Process:Day 1-10 โ†’ Check Day 5Bloomed: [โœ…, โŒ, โœ…, โŒ, โœ…] โ†’ 3 bouquets โœ…Search left: [1, 5]Continue until minimum found: Day 3Day 3 Verification:โœ“Bloomedโœ—Not yetโœ“Bloomedโœ—Not yetโœ“Bloomedโœ… Can make 3 bouquets (positions 0, 2, 4)
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

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space for binary search variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค bloomDay.length โ‰ค 105
  • 1 โ‰ค bloomDay[i] โ‰ค 109
  • 1 โ‰ค m, k โ‰ค bloomDay.length
  • Important: Each bouquet needs exactly k adjacent flowers
Asked in
Google 28 Amazon 22 Microsoft 15 Meta 12
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen