
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find minimum number of days to make m bouquets using Python
Suppose we have an array with integers called nums, we also have another two values m and k. Now, we need to make m bouquets. To make one bouquet we need k adjacent flowers from the garden. Here the garden consists of n different flowers, the ith flower will bloom in the bloomDay[i]. Each flower can be used inside only one bouquets. We have to find the minimum number of days need to wait to make m bouquets from the garden. If we cannot make m bouquets, then return -1.
So, if the input is like bloomDay = [5,5,5,5,10,5,5] m = 2 k = 3, then the output will be 10 because we need 2 (m = 2) bouquets and each should have 3 flowers.
After day 5: [x, x, x, x, _, x, x], we can make one bouquet of 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.
To solve this, we will follow these steps −
n := size of bloomDay
if m * k > n, then
return -1
Define a function possible() . This will take x
count := 0, bouquets := 0
for each d in bloomDay, do
if d <= x, then
count := count + 1
if count is same as k, then
bouquets := bouquets + 1
count := 0
otherwise,
count := 0
return true if bouquets >= m, otherwise false
From the main method do the following −
left := 0, right := 1 + maximum of bloomDay
while left < right, do
mid :=(left + right) /2
if possible(mid) is true, then
right := mid
otherwise,
left := mid + 1
if possible(left) is true, then
return left
otherwise return left + 1
Let us see the following implementation to get better understanding −
Example
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 if possible(left): return left else: return left + 1 bloomDay = [5,5,5,5,10,5,5] m = 2 k = 3 print(solve(bloomDay, m, k))
Input
[5,5,5,5,10,5,5], 2, 3
Output
10
- Related Articles
- Program to find minimum number of days to wait to make profit in python
- Program to find minimum number of days to eat N oranges in Python
- Program to find minimum number of operations to make string sorted in Python
- Program to find minimum operations to make array equal using Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum operations to make the array increasing using Python
- Program to find minimum numbers of function calls to make target array using Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find minimum number of characters to be added to make it palindrome in Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to find minimum moves to make array complementary in Python
- Program to find minimum deletions to make string balanced in Python
