Posted on

Description

Submission

class Solution {
    bool checkOK(vector<int>& bloomDay, int k, int m, int d) {
        int counter = 0; // consecutive days
        for(int x: bloomDay) {
            if(x <= d) counter++;
            else counter = 0;
            if(counter >= k) {
                counter -= k;
                m--;
                if(m == 0) return true;
            }
        }
        return false;
    }
public:
    int minDays(vector<int>& bloomDay, int m, int k) {
        int n = bloomDay.size();
        int maxVal = *max_element(bloomDay.begin(), bloomDay.end());
        int left = 0, right = maxVal + 1;

        while(left < right) {
            int mid = left + (right - left) / 2;

            if(checkOK(bloomDay, k, m, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }

        if(left > maxVal) return -1;

        return left;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *