【LeetCode】1482. The minimum number of days required to make n bouquets of flowers

topic

You are given an integer array bloomDay, and two integers m and k.
Now it is necessary to make m bouquets of flowers. When making a bouquet, you need to use k adjacent flowers in the garden.
There are n flowers in the garden, and the i-th flower will bloom on bloomDay[i], which happens to be used in a bunch of flowers.
Please return the minimum number of days you need to wait to pick m bouquets of flowers from the garden. If m bouquets cannot be picked, return -1.

Example 1:

Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let us observe the blooming process of these three days together, x means blooming, and _ means blooming Not open.
Now you need to make 3 bouquets, only 1 flower in each bouquet.
After 1 day: [x, _, _, _, _] // Only 1 flower can be made After
2 days: [x, _, _, _, x] // Only 2 flowers can be made After
3 days: [x, _, x, _, x] // can make 3 bouquets, the answer is 3

Example 2:

Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: To make 3 bunches of flowers, each bunch needs 2 flowers, that is, a total of 6 flowers are needed. And there are only 5 flowers in the garden, which cannot meet the production requirements, and return -1.

Example 3:

Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: To make 2 bouquets, each bouquet needs 3 flowers.
The garden looks like this after 7 and 12 days: After
7 days: [x, x, x, x, _, x, x] The
first 3 blooms can be used to make the first bouquet. But the last 3 blooming flowers cannot be used because they are not adjacent.
12 days later: [x, x, x, x, x, x, x]
Obviously, we can make the two bouquets in different ways.

Example 4:

Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: It takes 1000000000 days to pick flowers to make a bouquet

Example 5:

Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9

hint:

bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n

answer

Dichotomy
The boundary value is 1, and the maximum value of the bloomDay sequence
is mid (the bouquet is made after waiting for mid days)
and then traverse the sequence to obtain the number of bouquets that meet the conditions.
Change the boundary value

class Solution {
    
    
public:
    int minDays(vector<int>& bloomDay, int m, int k) {
    
    
        int len = bloomDay.size();
        if(m*k > len)
            return -1;

        int left = 0;
        //int right = *max_element(bloomDay.cbegin(),bloomDay.cend());
        int right = 1e+9 +1;

        int res = right;

        while(left<=right)
        {
    
    
            int mid = left + ((right-left)>>1);

            int cnt = 0;
            int flower = 0;
            for(int i:bloomDay)
            {
    
    
                if(i <= mid)
                    flower++;
                else
                    flower=0;
                //cout<<i<<"==="<<flower<<endl;
                if(flower == k)
                {
    
    
                    cnt++;
                    flower=0;
                }
            }

            //cout<<left<<"=="<<mid<<"=="<<right<<"==>"<<cnt<<endl;
            if(cnt>=m)
            {
    
    
                right = mid-1;
                res = mid;
            }
            else
                left = mid+1;
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/qq_45972928/article/details/126294924