AlgoViz

Minimum Days to Make M Bouquets

Medium

Search the bloom day

In simple words

Guess a number of days; check if that's enough to make all the bouquets, then try fewer or more days.

The idea

More days can only help, so 'can we make m bouquets by day d' is monotone. Binary search the earliest such day.

1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
candidates10

Step 1 of 5. Brute force: try every day until 3 bouquet(s) can bloom. Values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. candidates 10.

1/5
Brute force
timeO(max·n)spaceO(1)

Try every day.

1let best = -1;2for (let day = minBloom; day <= maxBloom && best < 0; day++)3  if (bouquets(day) >= m) best = day;4return best;

Input

array
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Memory

try
candidates
10
day

Output

day
answer

Check yourself

3 quick questions about this walkthrough. A wrong answer costs nothing.

Examples

Example 1

Input:
bloom = [1, 10, 3, 10, 2], m = 3, k = 1
Output:
3
Explanation:
By day 3, three single flowers have bloomed.

Example 2

Input:
bloom = [1, 10, 3, 10, 2], m = 3, k = 2
Output:
-1
Explanation:
Not enough adjacent pairs ever bloom → -1.

Example 3

Input:
bloom = [7, 7, 7, 7, 13, 11, 12, 7], m = 2, k = 3
Output:
12
Explanation:
Day 12 lets two groups of 3 adjacent flowers bloom.

Finished the walkthrough? Add it to your streak.