Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array bloomDay, an integer m and an integer k.
You want 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 us see what happened in the first three days. x means flower bloomed and _ means flower did not 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 is 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.
Constraints:
bloomDay.length == n1 <= n <= 1051 <= bloomDay[i] <= 1091 <= m <= 1061 <= k <= nProblem summary: You are given an integer array bloomDay, an integer m and an integer k. You want 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[1,10,3,10,2] 3 1
[1,10,3,10,2] 3 2
[7,7,7,7,12,7,7] 2 3
maximize-the-confusion-of-an-exam)earliest-possible-day-of-full-bloom)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1482: Minimum Number of Days to Make m Bouquets
class Solution {
private int[] bloomDay;
private int m, k;
public int minDays(int[] bloomDay, int m, int k) {
this.bloomDay = bloomDay;
this.m = m;
this.k = k;
final int mx = (int) 1e9;
int l = 1, r = mx + 1;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l > mx ? -1 : l;
}
private boolean check(int days) {
int cnt = 0, cur = 0;
for (int x : bloomDay) {
cur = x <= days ? cur + 1 : 0;
if (cur == k) {
++cnt;
cur = 0;
}
}
return cnt >= m;
}
}
// Accepted solution for LeetCode #1482: Minimum Number of Days to Make m Bouquets
func minDays(bloomDay []int, m int, k int) int {
mx := slices.Max(bloomDay)
if l := sort.Search(mx+2, func(days int) bool {
cnt, cur := 0, 0
for _, x := range bloomDay {
if x <= days {
cur++
if cur == k {
cnt++
cur = 0
}
} else {
cur = 0
}
}
return cnt >= m
}); l <= mx {
return l
}
return -1
}
# Accepted solution for LeetCode #1482: Minimum Number of Days to Make m Bouquets
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def check(days: int) -> int:
cnt = cur = 0
for x in bloomDay:
cur = cur + 1 if x <= days else 0
if cur == k:
cnt += 1
cur = 0
return cnt >= m
mx = max(bloomDay)
l = bisect_left(range(mx + 2), True, key=check)
return -1 if l > mx else l
// Accepted solution for LeetCode #1482: Minimum Number of Days to Make m Bouquets
struct Solution;
impl Solution {
fn min_days(bloom_day: Vec<i32>, m: i32, k: i32) -> i32 {
let m = m;
let k = k as usize;
let n = bloom_day.len();
if n < m as usize * k {
return -1;
}
let mut left = 1;
let mut right = std::i32::MAX;
while left < right {
let mid = left + (right - left) / 2;
let mut group = 0;
let mut count = 0;
for i in 0..n {
if bloom_day[i] > mid {
count = 0;
} else {
count += 1;
if count >= k {
count = 0;
group += 1;
}
}
}
if group < m {
left = mid + 1;
} else {
right = mid;
}
}
left
}
}
#[test]
fn test() {
let bloom_day = vec![1, 10, 3, 10, 2];
let m = 3;
let k = 1;
let res = 3;
assert_eq!(Solution::min_days(bloom_day, m, k), res);
let bloom_day = vec![1, 10, 3, 10, 2];
let m = 3;
let k = 2;
let res = -1;
assert_eq!(Solution::min_days(bloom_day, m, k), res);
let bloom_day = vec![7, 7, 7, 7, 12, 7, 7];
let m = 2;
let k = 3;
let res = 12;
assert_eq!(Solution::min_days(bloom_day, m, k), res);
let bloom_day = vec![1000000000, 1000000000];
let m = 1;
let k = 1;
let res = 1000000000;
assert_eq!(Solution::min_days(bloom_day, m, k), res);
let bloom_day = vec![1, 10, 2, 9, 3, 8, 4, 7, 5, 6];
let m = 4;
let k = 2;
let res = 9;
assert_eq!(Solution::min_days(bloom_day, m, k), res);
}
// Accepted solution for LeetCode #1482: Minimum Number of Days to Make m Bouquets
function minDays(bloomDay: number[], m: number, k: number): number {
const mx = Math.max(...bloomDay);
let [l, r] = [1, mx + 1];
const check = (days: number): boolean => {
let [cnt, cur] = [0, 0];
for (const x of bloomDay) {
cur = x <= days ? cur + 1 : 0;
if (cur === k) {
cnt++;
cur = 0;
}
}
return cnt >= m;
};
while (l < r) {
const mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l > mx ? -1 : l;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.