You are given an integer array bloomDay
, 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 consists of n
flowers, the ith
flower will bloom in the bloomDay[i]
and then can be used in exactly 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
.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1 Output: 3 Explanation: Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2 Output: -1 Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 Output: 12 Explanation: We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways.
Constraints:
bloomDay.length == n
1 <= n <= 105
1 <= bloomDay[i] <= 109
1 <= m <= 106
1 <= k <= n
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the minimum number of days involves checking every possible number of days to see if we can make the required number of bouquets. We essentially test each day value one by one until we find the smallest one that works. It's like trying every number until we hit the right one.
Here's how the algorithm would work step-by-step:
def min_days_make_bouquets_brute_force(bloom_day, number_of_bouquets, flowers_per_bouquet):
days = 1
max_days = max(bloom_day)
while days <= max_days:
# Check if we can make the required bouquets with 'days'
possible = can_make_bouquets(bloom_day, days, number_of_bouquets, flowers_per_bouquet)
if possible:
return days
days += 1
return -1
def can_make_bouquets(bloom_day, current_day, number_of_bouquets, flowers_per_bouquet):
bouquets_made = 0
flowers_collected = 0
for bloom_value in bloom_day:
if bloom_value <= current_day:
flowers_collected += 1
if flowers_collected == flowers_per_bouquet:
bouquets_made += 1
flowers_collected = 0
else:
flowers_collected = 0
# Check if we have made enough bouquets
if bouquets_made >= number_of_bouquets:
return True
else:
return False
def min_days_to_make_m_bouquets(bloom_day, number_of_bouquets, flowers_per_bouquet):
if number_of_bouquets * flowers_per_bouquet > len(bloom_day):
return -1
min_possible_day = min(bloom_day)
# Start checking from the minimum possible bloom day.
for day_to_check in range(min_possible_day, max(bloom_day) + 1):
if can_make_bouquets(bloom_day, day_to_check, number_of_bouquets, flowers_per_bouquet):
# Found a possible day to make the bouquets.
return day_to_check
return -1
The optimal strategy avoids checking every possible combination of days. Instead, we use a process of elimination guided by the fact that the number of days must be within a certain range. This allows us to efficiently narrow down the possibilities to find the minimum number of days required.
Here's how the algorithm would work step-by-step:
def min_days_to_make_m_bouquets(bloom_day, number_of_bouquets, flowers_per_bouquet):
number_of_flowers = len(bloom_day)
if number_of_bouquets * flowers_per_bouquet > number_of_flowers:
return -1
minimum_bloom_day = min(bloom_day)
maximum_bloom_day = max(bloom_day)
while minimum_bloom_day < maximum_bloom_day:
mid_day = (minimum_bloom_day + maximum_bloom_day) // 2
#Check if it's possible to create enough bouquets
if can_make_bouquets(bloom_day, mid_day, number_of_bouquets, flowers_per_bouquet):
maximum_bloom_day = mid_day
else:
minimum_bloom_day = mid_day + 1
return minimum_bloom_day
def can_make_bouquets(bloom_day, current_day, number_of_bouquets, flowers_per_bouquet):
bouquets_made = 0
flowers_collected = 0
for day in bloom_day:
if day <= current_day:
flowers_collected += 1
if flowers_collected == flowers_per_bouquet:
bouquets_made += 1
flowers_collected = 0
else:
flowers_collected = 0
# Return True if we have made enough bouquets to fulfill the requirement
return bouquets_made >= number_of_bouquets
Case | How to Handle |
---|---|
Empty bloomDay array or invalid input (m or k <= 0) | Return -1 immediately as no bouquets can be formed with invalid input or no flowers. |
m * k exceeds the length of bloomDay | Return -1 immediately because it's impossible to create the required number of bouquets. |
bloomDay array contains only one element | Return the bloomDay if m and k are both 1, otherwise -1. |
All bloomDay values are the same | Binary search should still converge to the correct minimum day if a valid solution exists, otherwise return -1 if no solution. |
No possible solution exists (e.g., bloomDay values are too large) | Binary search will converge where left > right, indicating no solution; return -1. |
bloomDay array contains very large numbers potentially causing integer overflow in calculations | Ensure binary search mid calculation uses long or appropriate data type to prevent overflow. |
bloomDay array is sorted in descending order (worst case for some inefficient algorithms) | Binary search efficiency is unaffected by the order of bloomDay values because it only cares about minimum and maximum possible bloom days and checking for existence of valid bloom day within that range. |
Extreme values for m and k within the problem constraints (e.g., m = 1, n = 10^5, k = 10^5) | Binary search should handle large array sizes efficiently with O(log(max(bloomDay))) time complexity. |