Find the minimum day when it becomes possible to make exactly or at least bouquets, each requiring adjacent bloomed flowers.
Problem
You are given an array bloomDay, where bloomDay[i] is the day the flower at index i blooms.
A bouquet can be made only from adjacent flowers that have all bloomed by the same day.
Your task is to determine the minimum day on which it is possible to make bouquets. If it is impossible to make that many bouquets, return -1.
A flower can be used in at most one bouquet.
Intuition
For a fixed day, you can check whether enough bouquets can be formed from consecutive bloomed flowers. The key is that this feasibility test is monotonic with respect to the day, which makes the problem a good fit for binary search on the answer.
Input Format
- An integer array
bloomDay - Two integers
mandk
Output Format
- Return the minimum day as an integer
- Return
-1if makingmbouquets is impossible
Constraints
- Each bouquet needs exactly
kadjacent flowers - A flower may belong to at most one bouquet
- If
m * k > bloomDay.length, the answer is-1 - The answer is the smallest day
dsuch that at leastmbouquets can be formed using flowers with bloom day<= d
Example 1
Input
bloomDay = [1,10,3,10,2], m = 3, k = 1
Output
3
Explanation
By day 3, flowers at indices 0, 2, and 4 have bloomed, so 3 bouquets of size 1 can be made.
Example 2
Input
bloomDay = [1,10,3,10,2], m = 3, k = 2
Output
-1
Explanation
There are only 5 flowers total, but 3 bouquets of 2 flowers each require 6 flowers.
Show 1 more example
Example 3
Input
bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output
12
Explanation
By day 7, only one bouquet of 3 adjacent bloomed flowers can be formed. By day 12, all flowers are bloomed, allowing two bouquets.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.