题目描述
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.
The first line of input contains two integers M and K.
The second line contains N space-separated integers of bloomDay[i] array.
Example 1:
Input:
M = 2, K = 3
bloomDay = [5, 5, 5, 5, 10, 5, 5]
Output:
10
Explanation:
As 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,
taking 3 adjacent flowers in one bouquet.
Example 2:
Input:
M = 3, K = 2
bloomDay = [1, 10, 3, 10, 2]
Output:
-1
Explanation:
As 3 bouquets each having 2 flowers are needed, that means we need 6 flowers.
But there are only 5 flowers so it is impossible to get the needed bouquets
therefore -1 will be returned.
Your Task:
Complete the function int solve(), which takes integer M, K, and a list of N integers as input and returns the minimum number of days needed to wait to be able to make M bouquets from the garden.
Expected Time Complexity: O(N.log(maxVal)), where maxVal = max(bloomDay)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= M <= 10^{5}
1 <= K <= N
1 <= bloomDay[i] <= 10^{9}
输入格式
见题面
输出格式
见题面