Minimum Number of Days to Make m Bouquets

Medium

Topics
ArrayBinary Search

nums[i] is the day flower i blooms. You want a bouquets, and each bouquet needs b adjacent flowers that have already bloomed.

Each flower can be used in at most one bouquet. Return the earliest day on which all a bouquets can be made, or -1 if it is impossible.

LeetCode names the arguments bloomDay, m and k. The harness here supplies them as nums, a and b: a is how many bouquets you need and b is how many adjacent flowers each one takes.

Example 1

101013210324
bloomed by day 3still closed
Wanting 3 bouquets of 1 adjacent flower from these 5 flowers first works on day 3. A bouquet needs its flowers side by side, so an unbloomed flower in the middle of a run splits it rather than shortening it — which is why a run of length L yields L divided by 1 bouquets and the remainder is wasted. Waiting never closes a flower again, so every later day also works and the answer is the earliest one that does.
The values are bloom days, not positions, and each flower serves at most one bouquet.
Input:  nums = [1,10,3,10,2], a = 3, b = 1
Output: 3
Explanation: By day 3 the flowers blooming are at positions 0, 2 and 4, which is three bouquets of one flower each.

Example 2

101013210324
still closed
Wanting 3 bouquets of 2 adjacent flowers from these 5 flowers is impossible, so the answer is -1. The garden holds fewer flowers than 3 bouquets of 2 require, and no amount of waiting creates more.
Input:  nums = [1,10,3,10,2], a = 3, b = 2
Output: -1
Explanation: Three bouquets of two adjacent flowers needs six flowers and only five exist, so no day works.

Example 3

707172731247576
bloomed by day 12
Wanting 2 bouquets of 3 adjacent flowers from these 7 flowers first works on day 12.
Input:  nums = [7,7,7,7,12,7,7], a = 2, b = 3
Output: 12
Explanation: By day 7 the bloomed positions are 0,1,2,3 and 5,6 — one run of four and one of two, giving only one bouquet. Day 12 fills the gap.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= a <= 10^6
  • 1 <= b <= nums.length
Editorial — intuition, approach, complexity and pitfalls

Intuition

Checking a specific day is easy: the flowers that have bloomed are those whose day is at most that day, and you can count the bouquets by walking the garden and measuring each unbroken run of bloomed flowers. A run of length L yields L / b bouquets, since each bouquet consumes b adjacent flowers and the leftovers of a run cannot join across a gap.

The structure that makes the whole problem tractable is that this check is monotone. Waiting longer never un-blooms a flower, so if a day works, every later day works too. The set of workable days is therefore everything from some threshold onward.

That makes the answer searchable: narrow a range of candidate days by testing the middle one. The range runs from the earliest bloom day to the latest, since no earlier day has any flower and no later day adds one.

The impossibility test should come first and is pure arithmetic: a bouquets of b flowers need a * b flowers in total. If the garden is smaller, no day helps. That product can overflow a 32-bit integer at the stated limits, which is a real trap.

Approach

  1. If a * b exceeds the number of flowers, return -1. Compute that product in a wide type.
  2. Set the search range from the smallest bloom day to the largest.
  3. For a candidate day, walk the flowers counting the length of the current run of flowers blooming at or before it, resetting the run at any flower that has not bloomed.
  4. Each time a run reaches b, count a bouquet and reset the run to zero — those flowers are now used.
  5. If the bouquet count reaches a, the day works, so narrow the range to it or earlier. Otherwise search later.
  6. The range closes on the earliest workable day.

Walkthrough

nums = [7,7,7,7,12,7,7], a = 2, b = 3

flowers needed: 2 * 3 = 6, garden has 7, so it is possible

search range [7, 12]

   day 7: bloomed? 7<=7 yes, yes, yes, yes, 12<=7 NO, yes, yes
      positions  0 1 2 3 . 5 6
      run 1,2,3 -> bouquet, reset
      run 1 (position 3)
      position 4 not bloomed -> run resets to 0
      run 1,2 (positions 5,6)
      bouquets 1 < 2, so day 7 does not work

   day 9: same as day 7, since nothing new blooms
      bouquets 1 < 2, does not work

   day 12: every flower has bloomed
      run 1..7, bouquet at 3, reset, run 1..4, bouquet at 3
      bouquets 2 >= 2, day 12 works

range closes on 12

answer 12

why a run gives L / b and not more
   a run of 4 with b = 3 gives ONE bouquet. The leftover
   flower cannot pair with flowers across the gap, because
   a bouquet needs ADJACENT flowers.

the impossibility case
   nums = [1,10,3,10,2], a = 3, b = 2
   needs 3 * 2 = 6 flowers, garden has 5
   -> -1, with no search at all

Complexity

  • Time — O(n log(max - min)): each candidate day is checked with one walk of the garden, and the day range halves each time.
  • Space — O(1): counters only.

Common Pitfalls

  • Computing a * b in a 32-bit integer, which overflows and wrongly reports a day as possible.
  • Letting a run carry across an unbloomed flower. A bouquet needs adjacent flowers, so a gap resets the run to zero.
  • Not resetting the run after making a bouquet, which reuses flowers.
  • Searching over array indices rather than over day values. The answer is a day, and it need not be any particular flower’s bloom day — though it always is one, which is why the range can be the bloom days.
  • Returning the candidate from the middle of the range rather than the value the range closed on.

Full Solution — all languages

class Solution:
    def minDays(self, nums, a, b):
        # a * b can exceed a 32-bit integer at the limits; Python is fine
        # but the comparison belongs first either way.
        if a * b > len(nums):
            return -1

        def bouquets_by(day):
            made, run = 0, 0

            for bloom in nums:
                if bloom <= day:
                    run += 1
                    if run == b:
                        made += 1
                        run = 0        # those b flowers are now used
                else:
                    run = 0            # a gap breaks the run entirely

            return made

        lo, hi = min(nums), max(nums)

        while lo < hi:
            mid = lo + (hi - lo) // 2
            if bouquets_by(mid) >= a:
                hi = mid
            else:
                lo = mid + 1

        return lo
SensAI
Beta
Tap the mic to stop voice mode