Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum number of days to make m bouquets using Python
Suppose we have an array with integers called bloomDay, along with two values m and k. We need to make m bouquets, where each bouquet requires k adjacent flowers from the garden. The garden has n different flowers, and the ith flower will bloom on day bloomDay[i]. Each flower can be used in only one bouquet. We need to find the minimum number of days to wait to make m bouquets from the garden. If we cannot make m bouquets, return -1.
Problem Example
If the input is bloomDay = [5,5,5,5,10,5,5], m = 2, k = 3, then the output will be 10 because we need 2 bouquets and each should have 3 adjacent flowers ?
After day 5: [x, x, x, x, _, x, x], we can make one bouquet from the first three flowers that bloomed, but cannot make another bouquet
After day 10: [x, x, x, x, x, x, x], now we can make two bouquets in different ways
Algorithm Steps
To solve this problem, we use binary search on the number of days ?
First, check if it's possible to make
mbouquets: ifm * k > n, return-1Use binary search between 0 and the maximum bloom day + 1
For each mid value, check if we can make
mbouquets by that dayCount consecutive bloomed flowers to form bouquets
Implementation
def solve(bloomDay, m, k):
n = len(bloomDay)
# If impossible to make m bouquets
if m * k > n:
return -1
def possible(x):
count = 0
bouquets = 0
for d in bloomDay:
if d <= x:
count += 1
if count == k:
bouquets += 1
count = 0
else:
count = 0
return bouquets >= m
# Binary search on days
left, right = 0, max(bloomDay) + 1
while left < right:
mid = (left + right) // 2
if possible(mid):
right = mid
else:
left = mid + 1
return left if possible(left) else -1
# Test the function
bloomDay = [5, 5, 5, 5, 10, 5, 5]
m = 2
k = 3
result = solve(bloomDay, m, k)
print(f"Minimum days needed: {result}")
Minimum days needed: 10
How It Works
The algorithm uses binary search to find the minimum number of days ?
Helper function:
possible(x)checks if we can makembouquets by dayxConsecutive counting: We count consecutive bloomed flowers and form a bouquet when we have
kadjacent flowersBinary search: We search between 0 and the maximum bloom day to find the minimum valid day
Example with Different Input
def solve(bloomDay, m, k):
n = len(bloomDay)
if m * k > n:
return -1
def possible(x):
count = 0
bouquets = 0
for d in bloomDay:
if d <= x:
count += 1
if count == k:
bouquets += 1
count = 0
else:
count = 0
return bouquets >= m
left, right = 0, max(bloomDay) + 1
while left < right:
mid = (left + right) // 2
if possible(mid):
right = mid
else:
left = mid + 1
return left if possible(left) else -1
# Test with impossible case
bloomDay = [1, 10, 3, 10, 2]
m = 3
k = 2
result = solve(bloomDay, m, k)
print(f"Result: {result}")
# Test with another case
bloomDay = [7, 7, 7, 7, 12, 7, 7]
m = 2
k = 3
result = solve(bloomDay, m, k)
print(f"Result: {result}")
Result: -1 Result: 12
Time Complexity
The time complexity is O(n log(max_day)) where n is the length of bloomDay and max_day is the maximum value in the array. The space complexity is O(1) excluding the input array.
Conclusion
This problem efficiently uses binary search to find the minimum days needed to make bouquets. The key insight is that if we can make m bouquets by day x, we can also make them by any day greater than x, making binary search applicable.
