LeetCode #1482 Medium

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 × k can reach 10¹¹, which does not fit in a 32-bit integer.
Constraints
  • bloomDay.length == n
  • 1 <= n <= 10⁵
  • 1 <= bloomDay[i] <= 10⁹
  • 1 <= m <= 10⁶
  • 1 <= k <= n
binary-searcharraybinary-search-on-answer
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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¹¹.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
▶3 if m * k > len(bloomDay):
▶4 return -1
▶5 
▶6 def can_make(day):
▶7 bouquets = run = 0
▶8 for d in bloomDay:
▶9 if d <= day:
▶10 run += 1
▶11 if run == k:
▶12 bouquets += 1
▶13 run = 0
▶14 else:
▶15 run = 0
▶16 return bouquets >= m
▶17 
▶18 lo, hi = min(bloomDay), max(bloomDay)
▶19 while lo < hi:
▶20 mid = (lo + hi) // 2
▶21 if can_make(mid):
▶22 hi = mid
▶23 else:
▶24 lo = mid + 1
▶25 return lo
days12345678910bloom101013210324run–bouquets–search days 1 to 10
lo1
hi10
Before day 1 nothing has bloomed, and by day 10 every flower has, which is enough because 3 × 1 ≤ 5. So the answer lies in this range.
days12345test678910bloom101013210324bouquet 1run1 of 1bouquets1 of 3flower 0 open → run reaches 1, bouquet 1
day5
bouquets1
run0
Flower 0 bloomed on day 1, which is not after day 5. That makes 1 open flowers in a row, so they form bouquet 1. The run starts again at 0 so none of them is used twice.
days12345test678910bloom101013210324bouquet 1run0 of 1bouquets1 of 3flower 1 not open until day 10 → run stays 0
day5
run0
Flower 1 only blooms on day 10, after day 5. A bouquet cannot skip over it, so the run stays at 0.
days12345test678910bloom101013210324bouquet 1bouquet 2run1 of 1bouquets2 of 3flower 2 open → run reaches 1, bouquet 2
day5
bouquets2
run0
Flower 2 bloomed on day 3, which is not after day 5. That makes 1 open flowers in a row, so they form bouquet 2. The run starts again at 0 so none of them is used twice.
days12345test678910bloom101013210324bouquet 1bouquet 2run0 of 1bouquets2 of 3flower 3 not open until day 10 → run stays 0
day5
run0
Flower 3 only blooms on day 10, after day 5. A bouquet cannot skip over it, so the run stays at 0.
days12345test678910bloom101013210324bouquet 1bouquet 2bouquet 3run1 of 1bouquets3 of 3flower 4 open → run reaches 1, bouquet 3
day5
bouquets3
run0
Flower 4 bloomed on day 2, which is not after day 5. That makes 1 open flowers in a row, so they form bouquet 3. The run starts again at 0 so none of them is used twice.
days12345test678910bloom101013210324bouquet 1bouquet 2bouquet 3run0 of 1bouquets3 of 33 bouquets by day 5 → works, try earlier
hi5
Day 5 gives 3 bouquets, at least the 3 needed. It might be the answer, so it stays in range as the new upper end, and every later day is dropped.
days123test45678910bloom101013210324bouquet 1run1 of 1bouquets1 of 3flower 0 open → run reaches 1, bouquet 1
day3
bouquets1
run0
Flower 0 bloomed on day 1, which is not after day 3. That makes 1 open flowers in a row, so they form bouquet 1. The run starts again at 0 so none of them is used twice.
days123test45678910bloom101013210324bouquet 1run0 of 1bouquets1 of 3flower 1 not open until day 10 → run stays 0
day3
run0
Flower 1 only blooms on day 10, after day 3. A bouquet cannot skip over it, so the run stays at 0.
days123test45678910bloom101013210324bouquet 1bouquet 2run1 of 1bouquets2 of 3flower 2 open → run reaches 1, bouquet 2
day3
bouquets2
run0
Flower 2 bloomed on day 3, which is not after day 3. That makes 1 open flowers in a row, so they form bouquet 2. The run starts again at 0 so none of them is used twice.
days123test45678910bloom101013210324bouquet 1bouquet 2run0 of 1bouquets2 of 3flower 3 not open until day 10 → run stays 0
day3
run0
Flower 3 only blooms on day 10, after day 3. A bouquet cannot skip over it, so the run stays at 0.
days123test45678910bloom101013210324bouquet 1bouquet 2bouquet 3run1 of 1bouquets3 of 3flower 4 open → run reaches 1, bouquet 3
day3
bouquets3
run0
Flower 4 bloomed on day 2, which is not after day 3. That makes 1 open flowers in a row, so they form bouquet 3. The run starts again at 0 so none of them is used twice.
days123test45678910bloom101013210324bouquet 1bouquet 2bouquet 3run0 of 1bouquets3 of 33 bouquets by day 3 → works, try earlier
hi3
Day 3 gives 3 bouquets, at least the 3 needed. It might be the answer, so it stays in range as the new upper end, and every later day is dropped.
days12test345678910bloom101013210324bouquet 1run1 of 1bouquets1 of 3flower 0 open → run reaches 1, bouquet 1
day2
bouquets1
run0
Flower 0 bloomed on day 1, which is not after day 2. That makes 1 open flowers in a row, so they form bouquet 1. The run starts again at 0 so none of them is used twice.
days12test345678910bloom101013210324bouquet 1run0 of 1bouquets1 of 3flower 1 not open until day 10 → run stays 0
day2
run0
Flower 1 only blooms on day 10, after day 2. A bouquet cannot skip over it, so the run stays at 0.
days12test345678910bloom101013210324bouquet 1run0 of 1bouquets1 of 3flower 2 not open until day 3 → run stays 0
day2
run0
Flower 2 only blooms on day 3, after day 2. A bouquet cannot skip over it, so the run stays at 0.
days12test345678910bloom101013210324bouquet 1run0 of 1bouquets1 of 3flower 3 not open until day 10 → run stays 0
day2
run0
Flower 3 only blooms on day 10, after day 2. A bouquet cannot skip over it, so the run stays at 0.
days12test345678910bloom101013210324bouquet 1bouquet 2run1 of 1bouquets2 of 3flower 4 open → run reaches 1, bouquet 2
day2
bouquets2
run0
Flower 4 bloomed on day 2, which is not after day 2. That makes 1 open flowers in a row, so they form bouquet 2. The run starts again at 0 so none of them is used twice.
days12test345678910bloom101013210324bouquet 1bouquet 2run0 of 1bouquets2 of 3only 2 of 3 by day 2 → too early
lo3
Day 2 gives only 2 bouquets. Every earlier day has the same or fewer open flowers, so they all fail too, and the search moves past 2.
days12345678910bloom101013210324run–bouquets–answer: day 3
return3
Only day 3 is left in range. Every day after it was cut because an earlier day already worked, and every day before it failed, so 3 is the fewest days to wait.
05

Common pitfalls

Counting bloomed flowers without checking they are adjacent

✗ Wrong
bloomed = sum(1 for d in bloomDay if d <= day)
return bloomed // k >= m
✓ Right
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

✗ Wrong
if (m * k > bloomDay.size()) return -1;
✓ Right
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.

06

Edge cases

Exactly enough flowers: m × k == n

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.

k = 1

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.

07

Complexity

Time
O(n log D)
Space
O(1)
D is the gap between the earliest and latest bloom day, at most 10⁹, so the search takes about 30 rounds. Each round is one O(n) pass over the flowers, and only a few counters are kept.
08

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.