π6. Minimum days to make M bouquets π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
You have a row of flowers, where each flower blooms after a specific day. The array arr[] represents the blooming schedule, where arr[i] is the day the flower at position i will bloom. To create a bouquet, you need to collect k adjacent bloomed flowers. Each flower can only be used in one bouquet.
Your goal is to find the minimum number of days required to make exactly m bouquets. If it is not possible to make m bouquets with the given arrangement, return -1.
π Example Walkthrough
Input:
m = 3, k = 2, arr[] = [3, 4, 2, 7, 13, 8, 5]Output:
8Explanation: We need 3 bouquets, each containing 2 flowers. After day 8: [x, x, x, x, _, x, x], we can make:
First bouquet from flowers at indices 0 and 1,
Second bouquet from flowers at indices 2 and 3,
Third bouquet from flowers at indices 4 and 5.
After day 8, we can make 3 bouquets, so the answer is 8.
Input:
m = 2, k = 3, arr[] = [5, 5, 5, 5, 10, 5, 5]Output:
Explanation: We need 2 bouquets, each containing 3 flowers. After day 5: [x, x, x, x, _, x, x], we can make the first bouquet from the first 3 flowers. After day 10: [x, x, x, x, x, x, x], we can form the second bouquet. So the answer is 10.
Input:
Output:
Explanation:
To make 3 bouquets, each containing 2 flowers, we need a total of 6 flowers. However, there are only 5 flowers in the array, so it is impossible to create 3 bouquets. The answer is -1.
Constraints:
$1 β€ k β€ arr.size() β€ 10^5$
$1 β€ m β€ 10^5$
$1 β€ arr[i] β€ 10^9$
π― My Approach:
Step-by-Step Explanation:
Initial Check:
If
m * k > arr.size(), it's impossible to formmbouquets because we need at leastm * kflowers. Return-1immediately.
Binary Search for Minimum Days:
The key idea here is to use binary search on the number of days to find the minimum day in which we can form exactly
mbouquets.The search range is between the earliest blooming day (1) and the latest blooming day (the maximum value in the array).
Greedy Approach for Bouquet Formation:
For each day in the binary search, we simulate the process of making bouquets:
Traverse the array and count adjacent bloomed flowers.
Once we collect
kadjacent flowers, treat them as a bouquet and reset the count.If we can form
mbouquets, continue searching in the left half of the days, else search in the right half.
Termination Condition:
The binary search stops when
low == high, at which pointlowrepresents the minimum day on which it's possible to form exactlymbouquets.
π Time and Auxiliary Space Complexity
Time Complexity:
Binary search runs for
O(log(max(arr)))iterations, wheremax(arr)is the maximum number in the array.For each binary search iteration, we scan the entire array (
O(n)), so the overall time complexity isO(n * log(max(arr))).
Space Complexity:
The space complexity is
O(n)as we use a constant amount of extra space during the binary search and simulation process.
π Solution Code
Code (Cpp)
Code (Java)
Code (Python)
π’ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated