You are given an integer array bloom_day, an integer m and an integer k.
You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden has n flowers in a row. Flower i blooms on day bloom_day[i], and after that it can be used in at most one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets, return -1.
Function Signature
def min_days(bloom_day: list[int], m: int, k: int) -> int:
Rules
-
Flower
i
is available on day
d
when
bloom_day[i] <= d
, and it stays available after that.
-
A bouquet is a block of
k
consecutive positions in
bloom_day
, all available on the chosen day. Two bouquets cannot share a flower.
-
Return the smallest day on which
m
bouquets can be made, or
-1
if no day works.
Constraints
-
len(bloom_day) == n
-
1 <= n <= 10^5
-
1 <= bloom_day[i] <= 10^9
-
1 <= m <= 10^6
-
1 <= k <= n
Examples
Input: bloom_day = [1,10,3,10,2], m = 3, k = 1
Output: 3
Each bouquet needs one flower. By day 3 the flowers that bloom on days 1, 3 and 2 are open, which gives 3 bouquets. On day 2 only two flowers are open.
Input: bloom_day = [1,10,3,10,2], m = 3, k = 2
Output: -1
Three bouquets of 2 adjacent flowers need 6 flowers, and the garden has only 5.
Input: bloom_day = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
On day 7 every flower except the one at index 4 is open, so the open flowers form runs of length 4 and 2. That allows only one bouquet of 3. On day 12 all 7 flowers form one run, which gives 2 bouquets.