Leetcode 1482. Minimum Number of Days to Make m Bouquets
Find the minimum day such that you can form at least m bouquets, each requiring k adjacent flowers that have bloomed (return -1 if impossible, e.g., when m*k > n). This is typically solved by binary searching the day and greedily scanning for k-length contiguous bloomed segments to check feasibility.
Asked at:
Amazon
Meta
Apple
DESCRIPTION
Find the minimum day such that you can form at least m bouquets, each requiring k adjacent flowers that have bloomed (return -1 if impossible, e.g., when m*k > n). This is typically solved by binary searching the day and greedily scanning for k-length contiguous bloomed segments to check feasibility.
Input:
bloomDay = [1,10,3,10,2], m = 3, k = 1
Output:
3
Explanation: On day 3, flowers at positions 0, 2, and 4 have bloomed. Since k=1, each flower forms a bouquet. We can make 3 bouquets.
Constraints:
- bloomDay.length == n
- 1 <= n <= 10^5
- 1 <= bloomDay[i] <= 10^9
- 1 <= m <= 10^6
- 1 <= k <= n
- If m * k > n, return -1 (impossible to form enough bouquets)
Understanding the Problem
Let's understand what we're being asked to do. We have an array bloomDay where bloomDay[i] represents the day when flower i blooms. We need to form m bouquets, where each bouquet requires k adjacent flowers that have all bloomed.
For example, with bloomDay = [1,10,3,10,2], m = 3, k = 1: On day 1, flower 0 blooms. On day 2, flowers 0 and 4 have bloomed. On day 3, flowers 0, 2, and 4 have bloomed - we can make 3 bouquets (each needs only 1 flower). Answer: day 3.
Now consider bloomDay = [1,10,3,10,2], m = 3, k = 2: We need 3 bouquets, each requiring 2 adjacent flowers. Even on day 10 (all flowers bloomed), we can't form 3 bouquets of 2 adjacent flowers from 5 flowers. Answer: -1.
Key constraint: Flowers must be adjacent (consecutive positions in the array) to form a bouquet. We can't pick flowers from different parts of the array.
Edge cases: What if m * k > n (impossible to form enough bouquets)? Return -1. What if multiple days work? Return the minimum day.
Brute Force Approach
The brute force approach checks every possible day from 1 to max(bloomDay) sequentially. For each day, scan the bloomDay array to count how many contiguous segments of k bloomed flowers exist. If we can form at least m bouquets, return that day. This approach is simple but inefficient because it checks every single day, even though many days will give the same result.
Start brute force: check each day sequentially
0 / 86
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n * max(bloomDay)) For each of the max(bloomDay) possible days, we scan the array of n flowers to check feasibility. This results in O(n * max(bloomDay)) time, which can be extremely large when max(bloomDay) is up to 10^9.
Space Complexity: O(1) We only use a constant amount of extra space for counters and variables, regardless of input size.
Building Intuition
The key insight is that if we can make m bouquets on day D, we can also make m bouquets on any day after D (more flowers will have bloomed).
This creates a monotonic property: there's a threshold day where the answer changes from 'impossible' to 'possible'. Days before this threshold fail, days after succeed.
When we have a sorted search space with a clear 'yes/no' boundary, we can use binary search on the answer.
Instead of checking every day from 1 to max(bloomDay), we can eliminate half the days with each check, reducing from O(n) checks to O(log n) checks.
Think of it like finding the exact temperature where water freezes. Below 0°C it's frozen, above 0°C it's liquid.
You don't need to test every temperature from -100°C to +100°C. Instead, test the middle temperature, see if it's frozen or liquid, and narrow your search to the relevant half. The monotonic property (frozen → liquid, never liquid → frozen → liquid) makes this efficient.
For each candidate day, we need to check: 'Can we form m bouquets by this day?' This check involves scanning the array to count contiguous bloomed segments of length k.
Common Mistakes
Optimal Solution
The optimal approach uses binary search on the answer space (days from 1 to max(bloomDay)). For each candidate day (mid), we check feasibility by scanning the array once to count contiguous bloomed segments of length k. If we can form at least m bouquets, search the left half for an earlier day; otherwise, search the right half. This leverages the monotonic property: if day D works, all days after D also work.
Start: Find minimum days to make m bouquets of k adjacent flowers
0 / 83
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n * log(max(bloomDay))) Binary search on the day range takes O(log(max(bloomDay))) iterations. Each iteration requires O(n) time to scan the array and check feasibility. Total: O(n * log(max(bloomDay))). With max(bloomDay) up to 10^9, log(10^9) ≈ 30, making this much faster than the brute force.
Space Complexity: O(1) We only use a constant amount of extra space for binary search pointers (left, right, mid) and the feasibility check counters.
What We've Learned
- Binary search on answer space: When a problem asks for a "minimum/maximum value that satisfies a condition" and you can verify feasibility in reasonable time, think binary search on the answer rather than the input array - here we search days [1, max(bloomDay)] instead of array indices.
- Greedy feasibility checking: The verification function uses a greedy scan to count bouquets by tracking consecutive bloomed flowers - reset your counter when the chain breaks and increment bouquet count when reaching k adjacent flowers, making each check O(n) instead of exploring all combinations.
- Monotonicity recognition: Binary search works here because of the monotonic property - if you can make m bouquets on day X, you can definitely make them on day X+1 or later. Always verify this "if true at X, then true for all Y > X" property before applying binary search.
- Overflow prevention in constraints: Check impossibility upfront with `m * k > n` before starting binary search - this prevents wasted computation and handles the edge case where the problem is unsolvable, but be careful with integer overflow when m and k are large (use `m > n/k` instead).
- Search space boundary selection: Set binary search bounds as `left = min(bloomDay)` and `right = max(bloomDay)` rather than arbitrary values - this tightens the search space and ensures you're only checking valid day values that actually appear in the problem.
- Simulation-based optimization pattern: This "binary search + simulation" pattern applies broadly to resource allocation problems (minimum capacity, minimum time, minimum speed) where direct calculation is hard but checking "is X enough?" is straightforward - look for this in scheduling, capacity, and threshold problems.
Related Concepts and Problems to Practice
This lesson introduces binary search fundamentals, which is the core technique used in the bouquet problem. Understanding binary search on answer space is essential for solving optimization problems where you search for a minimum/maximum value that satisfies certain conditions.
medium
This problem uses the exact same pattern as the bouquet problem: binary search on the answer combined with a greedy feasibility check. Both problems require finding a minimum value (eating speed vs. days) where a condition can be satisfied, making it the most directly similar problem available.
easy
While the bouquet problem uses binary search, the feasibility check involves scanning for k-length contiguous segments of bloomed flowers, which is conceptually similar to fixed-size sliding window problems. This helps practice the greedy scanning component of the solution.
Test Your Understanding
Why is array the right data structure for this problem?
array provides the optimal access pattern
It's the only data structure that works
It's the easiest to implement
It uses the least memory
Select an answer to see the explanation
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Mid February, 2025
Meta
Senior
Early February, 2025
Apple
Senior
Mid January, 2025
Amazon
Senior
Minimum number of days to make m bouquets
Comments
Hello Interview Premium
Your account is free and you can post anonymously if you choose.