Minimum Number of Days to Make m Bouquets
Medium
Topics
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
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
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
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
- If
a * bexceeds the number of flowers, return-1. Compute that product in a wide type. - Set the search range from the smallest bloom day to the largest.
- 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.
- Each time a run reaches
b, count a bouquet and reset the run to zero — those flowers are now used. - If the bouquet count reaches
a, the day works, so narrow the range to it or earlier. Otherwise search later. - 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 allComplexity
- 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 * bin 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 loimport java.util.*;
class Solution {
public int minDays(int[] nums, int a, int b) {
// The product overflows a 32-bit int at the stated limits.
if ((long) a * b > nums.length) return -1;
int lo = Integer.MAX_VALUE, hi = Integer.MIN_VALUE;
for (int v : nums) {
lo = Math.min(lo, v);
hi = Math.max(hi, v);
}
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
int made = 0, run = 0;
for (int bloom : nums) {
if (bloom <= mid) {
run++;
if (run == b) {
made++;
run = 0;
}
} else {
run = 0;
}
}
if (made >= a) hi = mid;
else lo = mid + 1;
}
return lo;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minDays(vector<int>& nums, int a, int b) {
// The product overflows a 32-bit int at the stated limits.
if ((long long)a * b > (long long)nums.size()) return -1;
auto bouquetsBy = [&](int day) {
int made = 0, run = 0;
for (int bloom : nums) {
if (bloom <= day) {
run++;
if (run == b) {
made++;
run = 0;
}
} else {
run = 0;
}
}
return made;
};
int lo = *min_element(nums.begin(), nums.end());
int hi = *max_element(nums.begin(), nums.end());
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (bouquetsBy(mid) >= a) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} a
* @param {number} b
* @return {number}
*/ var minDays = function(nums, a, b) {
if (a * b > nums.length) return -1;
const bouquetsBy = (day) => {
let made = 0, run = 0;
for (const bloom of nums) {
if (bloom <= day) {
run++;
if (run === b) {
made++;
run = 0;
}
} else {
run = 0;
}
}
return made;
};
let lo = Math.min(...nums), hi = Math.max(...nums);
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (bouquetsBy(mid) >= a) hi = mid;
else lo = mid + 1;
}
return lo;
};