Minimum Number of Days to Make m Bouquets

Pattern: Binary search on the answer

medium

Guess a day and count the bouquets, runs of k flowers that have all bloomed by then. Later days only allow more bouquets, so binary search the earliest day that gives m of them.

The problem

Flower i blooms on day bloomDay[i]. A bouquet needs k flowers that are next to each other, all bloomed. Each flower can be in one bouquet only. Return the earliest day on which you can make m bouquets, or -1 if you never can. For example, [1, 10, 3, 10, 2] gives 3 for m = 3, k = 1 and -1 for m = 3, k = 2.

The function name minDays matches LeetCode. Use the link above for the exact statement and limits.

Think first

  • When is the answer -1 no matter how long you wait?
  • On a fixed day, how do you count bouquets in one pass?
  • If day d is enough, is day d + 1 enough?

Hints

    Brute force, and why it fails

    Try every distinct bloom day in increasing order and count bouquets each time: O(n²). Binary search over days: O(n log maxDay).

    The key insight

    Waiting longer can only help. That makes "is day d enough" monotonic, so the earliest good day is a binary search, and the check is one greedy pass.

    Dry run

    10 
    101lo
    32 
    103 
    24 

    bloomDay = [1, 10, 3, 10, 2], m = 3 bouquets, k = 1 flower each. 3 × 1 ≤ 5 flowers, so it is possible. Days 1 to 10.

    Step 1 of 4

    The plan

    if m * k > n: return -1
    lo = min(bloomDay), hi = max(bloomDay)
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if bouquets(mid) >= m: hi = mid
        else: lo = mid + 1
    return lo
    
    bouquets(day): made = 0, run = 0
        for each d in bloomDay:
            if d <= day: run = run + 1; if run == k: made = made + 1; run = 0
            else: run = 0
        return made
    

    Complexity

    Time
    O(n log maxDay)
    Space
    O(1)

    About 30 guesses for days up to 10⁹, each checked in O(n).

    Edge cases

    • m × k larger than n, even beyond 32 bits: -1.
    • k = 1: you need any m bloomed flowers.
    • [7, 7, 7, 7, 12, 7, 7] with m = 2, k = 3: the 12 splits the row, so the answer is 12.
    I've built my logic, show the code

    Java

    class Solution {
        public int minDays(int[] bloomDay, int m, int k) {
            if ((long) m * k > bloomDay.length) return -1;   // not enough flowers at all
            int lo = Integer.MAX_VALUE, hi = 0;
            for (int d : bloomDay) { lo = Math.min(lo, d); hi = Math.max(hi, d); }
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (bouquets(bloomDay, mid, k) >= m) hi = mid;   // enough by day mid: try earlier
                else lo = mid + 1;
            }
            return lo;
        }
    
        private int bouquets(int[] bloomDay, int day, int k) {
            int made = 0, run = 0;                  // run = bloomed flowers in a row right now
            for (int d : bloomDay) {
                if (d <= day) {
                    run++;
                    if (run == k) { made++; run = 0; }   // k in a row make one bouquet
                } else {
                    run = 0;                        // a flower not yet open breaks the row
                }
            }
            return made;
        }
    }

    C++

    class Solution {
    public:
        int minDays(vector<int>& bloomDay, int m, int k) {
            if (static_cast<long long>(m) * k > static_cast<long long>(bloomDay.size())) return -1;   // too few flowers
            int lo = *min_element(bloomDay.begin(), bloomDay.end());
            int hi = *max_element(bloomDay.begin(), bloomDay.end());
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (bouquets(bloomDay, mid, k) >= m) hi = mid;   // enough by day mid: try earlier
                else lo = mid + 1;
            }
            return lo;
        }
    
        int bouquets(vector<int>& bloomDay, int day, int k) {
            int made = 0, run = 0;                  // run = bloomed flowers in a row right now
            for (int d : bloomDay) {
                if (d <= day) {
                    run++;
                    if (run == k) { made++; run = 0; }   // k in a row make one bouquet
                } else {
                    run = 0;                        // a flower not yet open breaks the row
                }
            }
            return made;
        }
    };

    Python

    class Solution:
        def minDays(self, bloomDay: list[int], m: int, k: int) -> int:
            if m * k > len(bloomDay):
                return -1                           # not enough flowers at all
            lo, hi = min(bloomDay), max(bloomDay)
            while lo < hi:
                mid = (lo + hi) // 2
                if self.bouquets(bloomDay, mid, k) >= m:
                    hi = mid                        # enough by day mid: try earlier
                else:
                    lo = mid + 1
            return lo
    
        def bouquets(self, bloomDay: list[int], day: int, k: int) -> int:
            made = run = 0                          # run = bloomed flowers in a row right now
            for d in bloomDay:
                if d <= day:
                    run += 1
                    if run == k:                    # k in a row make one bouquet
                        made += 1
                        run = 0
                else:
                    run = 0                         # a flower not yet open breaks the row
            return made

    Common mistakes

    • Counting all bloomed flowers instead of runs of adjacent ones.
    • Forgetting to reset the run after a bouquet, so one flower is used twice.
    • Computing m * k in 32 bits, which overflows for large inputs.

    Similar problems