oneby7

Binary Search Searching the answer

246. Minimum Number of Days to Make m Bouquets

Medium

A garden has a row of flowers. Flower i opens on day bloomDay[i] and stays open from then on, so on any day d the open flowers are those with bloomDay[i] ≤ d.

You want to make m bouquets. Each bouquet takes k open flowers that stand next to each other in the row. A flower can go into only one bouquet, and flowers left over are fine.

Return the earliest day on which you can make all m bouquets. If the garden can never give that many, return -1.

Example 1

Input: bloomDay = [4, 9, 2, 7, 3, 8, 5], m = 2, k = 2

Output: 8

Explanation: On day 8 every flower except flower 1 is open. Flowers 2 to 6 stand together, so they make two bouquets: flowers 2 and 3, and flowers 4 and 5. On day 7 flower 5 is still closed, and only one bouquet fits.

Example 2

Input: bloomDay = [1, 1, 1, 1, 1], m = 3, k = 2

Output: -1

Explanation: Three bouquets of two need six flowers, and the garden has only five.

Example 3

Input: bloomDay = [10, 1000000000, 10, 10], m = 2, k = 2

Output: 1000000000

Explanation: Two bouquets of two use all four flowers, so we must wait for flower 1, which opens on day 1000000000.

Check your understanding

Work out the answer for this input before you write any code.

bloomDay = [2, 9, 2, 9, 2, 9, 2, 6, 6], m = 2, k = 2

Constraints

  • 1 ≤ bloomDay.length ≤ 105
  • 1 ≤ bloomDay[i] ≤ 109
  • 1 ≤ m ≤ 106
  • 1 ≤ k ≤ 106

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.