#1482

Minimum Number of Days to Make m Bouquets

Medium Binary search on the answer

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Flower i blooms on bloomDay[i]. A bouquet needs k adjacent bloomed flowers. Return the earliest day you can make m bouquets, or -1.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the earliest day you can make m bouquets of k neighbouring flowers.

Why this works: waiting longer never hurts, so binary search the day and count bouquets from runs of bloomed flowers.

Example: bloomDay [1, 10, 3, 10, 2], m = 3, k = 1

day 3: bloomed 1, 3, 2 → 3 bouquets ✓day 2: only 2 bouquets ✗ → answer 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Computing m * k in int — both can be 10^5 / 10^6 and overflow. Use long.

Solve on LeetCode ↗
Complexity
  • Time: O(n log D).
  • Space: O(1).
Topic: Binary Search
🎬 Episodes 21–23: video coming soon