Code Logo

Minimum Days to Make M Bouquets

Published at24 Jul 2026
Medium 0 views
Like0

Given an integer array bloomDay where bloomDay[i] is the day the i-th flower blooms, an integer m representing the number of bouquets needed, and an integer k representing the number of adjacent flowers required per bouquet, return the minimum number of days to make m bouquets. If it is impossible, return -1.

This is a classic binary search on answer problem. The minimum possible day is the minimum bloomDay (at least one flower blooms), and the maximum is the maximum bloomDay (all flowers bloom). Binary search across this range to find the smallest day where at least m bouquets of k adjacent flowers can be formed.

Feasibility is checked by scanning the garden: iterate through flowers, counting consecutive bloomed flowers (those with bloomDay <= current day). When the count reaches k, form a bouquet, reset the counter, and increment the bouquet count. At the end, check if the bouquet count is at least m.

If m * k > total flowers, it is impossible from the start and return -1 immediately. This is a fast-path optimization before any binary search.

The adjacency requirement (k adjacent flowers per bouquet) makes this problem distinct from simple counting. The scan must track consecutive bloomed flowers and reset on gaps. The binary search on answer pattern finds the earliest day satisfying both the minimum bloom condition and the adjacency constraint.

Example Input & Output

Example 1
Input
[1,10,3,10,2], 3, 2
Output
-1
Explanation

Need 6 flowers but only 5 exist.

Example 2
Input
[5,5,5,5], 1, 4
Output
5
Explanation

All four in one bouquet on day 5.

Example 3
Input
[1,2,3,4], 1, 1
Output
1
Explanation

Single bouquet from first flower on day 1.

Example 4
Input
[7,7,7,7,12,7,7], 2, 3
Output
12
Explanation

On day 12, all bloom: [7,7,7] and [7,12,7]...

Example 5
Input
[1,10,3,10,2], 3, 1
Output
3
Explanation

On day 3, flowers at indices 0,2,4 bloom → 3 bouquets.

Algorithm Flow

Recommendation Algorithm Flow for Minimum Days to Make M Bouquets
Recommendation Algorithm Flow for Minimum Days to Make M Bouquets

Solution Approach

Binary search between min and max bloomDay. Feasibility: scan garden counting consecutive bloomed flowers to form bouquets.

function solution(bloomDay, m, k) {
  if (m * k > bloomDay.length) return -1;
  var lo = bloomDay[0], hi = bloomDay[0];
  for (var i = 0; i < bloomDay.length; i++) {
    if (bloomDay[i] < lo) lo = bloomDay[i];
    if (bloomDay[i] > hi) hi = bloomDay[i];
  }
  function canMake(days) {
    var bouquets = 0, adj = 0;
    for (var i = 0; i < bloomDay.length; i++) {
      if (bloomDay[i] <= days) adj++;
      else adj = 0;
      if (adj === k) { bouquets++; adj = 0; }
    }
    return bouquets >= m;
  }
  while (lo < hi) {
    var mid = Math.floor((lo + hi) / 2);
    if (canMake(mid)) hi = mid;
    else lo = mid + 1;
  }
  return lo;
}

Time O(n log D), Space O(1).

Best Answers

java
class Solution {
    public int solution(int[] bloomDay, int m, int k) {
        if (m * k > bloomDay.length) return -1;
        int lo = bloomDay[0], hi = bloomDay[0];
        for (int d : bloomDay) { if (d < lo) lo = d; if (d > hi) hi = d; }
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (can(bloomDay, mid, m, k)) hi = mid;
            else lo = mid + 1;
        }
        return lo;
    }
    private boolean can(int[] d, int day, int m, int k) {
        int bouquets = 0, adj = 0;
        for (int v : d) {
            if (v <= day) adj++;
            else adj = 0;
            if (adj == k) { bouquets++; adj = 0; }
        }
        return bouquets >= m;
    }
}