Minimum Number of Days to Make m Bouquets

You are given an integer array bloomDay and two integers m and k. Flower i blooms on day bloomDay[i] and stays open afterwards. A bouquet needs k adjacent flowers from the garden, and each flower can go into at most one bouquet. Return the minimum number of days you need to wait to make m bouquets, or -1 if it is impossible.

30:00

Minimum Number of Days to Make m Bouquets
medium
Free written solution
Topics
Companies

You are given an integer array bloomDay and two integers m and k. Flower i blooms on day bloomDay[i] and stays open afterwards. A bouquet needs k adjacent flowers from the garden, and each flower can go into at most one bouquet. Return the minimum number of days you need to wait to make m bouquets, or -1 if it is impossible.

Example 1:
Input: {"bloomDay":[1,10,3,10,2],"m":3,"k":1}
Output: 3
Constraints:
  • bloomDay.length==n\text{bloomDay.length} == n

  • 1≤n≤1051 \leq n \leq 10^5

  • 1≤bloomDay[i]≤1091 \leq \text{bloomDay}[i] \leq 10^9

  • 1≤m≤1061 \leq m \leq 10^6

  • 1≤k≤n1 \leq k \leq n

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"bloomDay":[1,10,3,10,2],"m":3,"k":1}

The garden has 5 flowers; each label is the day that flower opens. We need 3 bouquets of 1 adjacent open flower.

Calendar: day 1 .. 10

lo=1
hi=10

● enough bouquets

● too early

Garden

run 0/1

1

0

10

1

3

2

10

3

2

4

● open (label <= day)

● still a bud

underline = bouquet

Bouquets

0 made / 3 needed

Variables
VariableValue
n5
m3
k1
DepthFunction Call
Stack empty
0/28