5455. Minimum Number of Days to Make m Bouquets
Given an integer array bloomDay
, an integer m
and an integer k
.
We need 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's see what happened in the first three days. x means flower bloomed and _ means flower didn't 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's 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.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1 Output: 1000000000 Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2 Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
花园里有n朵花,告诉你每朵花在第几天开花。现在要扎m束花,每束花必须由相邻的k朵花组成,问要完成这个任务,最少需要等多少天。
首先看看需要的总花数m*k是否>n,如果是的话,即使所有花都开了也无法完成任务。
对于能够完成的任务,要求最少等待天数。暴力方法是,枚举bloomDay数组中所有unique的天数,对于每一天,都看看能否完成任务,方法是把数组中相邻的开的花每k多组成一束,看看能否组成m束。
但是暴力方法超时,一个改进的方法是二分。因为天数是递增的,而且每增加一天,开花的总数也是递增的,相当于能组成的花束也是递增的(严格来说是非递减的),这就为二分搜索创造了条件。
维护两个数组,days存储了unique的天数,递增排序(map自动递增有序了);num_bloomed表示在当前天数下,开花的总数,随着days的增大, num_bloomed 存储的开花总数也是递增的。在构造 num_bloomed 的过程中,可以求到最小的可能的开始天数left,然后在left和right中二分搜索合法的最小天数。
完整代码如下:
class Solution {
bool IsValid(vector<int>& bloomDay, int day, int m, int k) {
int bouquets = 0, n = bloomDay.size();
int i = 0, good = 0;
while (i < n) {
while (i<n&&bloomDay[i]>day)++i;
if (i >= n)break;
int j = i;
while (j < n&&bloomDay[j] <= day)++j;
// [i,j) 都开花了
good += (j - i) / k;
i = j;
}
return good >= m;
}
public:
int minDays(vector<int>& bloomDay, int m, int k) {
int n = bloomDay.size();
if (m*k > n)return -1;
map<int, int> hash;
for (int i = 0; i < n; ++i)++hash[bloomDay[i]];
if (m*k == n)return (--hash.end())->first; // 最后一天
int unique_day = hash.size();
vector<int> days(unique_day, 0);
vector<int> num_bloomed(unique_day, 0);
int d = 0, nb = 0;
int left = 0, right = unique_day - 1, found_left = false;
for (map<int, int>::iterator it = hash.begin(); it != hash.end(); ++it) {
nb += it->second;
if (nb >= m * k && !found_left) {
left = d;
found_left = true;
}
days[d] = it->first;
num_bloomed[d] = nb;
++d;
}
while (left <= right) {
int mid = left + (right - left) / 2;
bool valid = IsValid(bloomDay, days[mid], m, k);
if (valid)right = mid - 1;
else left = mid + 1;
}
return days[right + 1];
}
};
本代码提交AC,用时1316MS。