Minimum Wait Days to Harvest Flower Bundles
Implement minHarvestDay
A row of plants blooms on different days —
bloomDay[i] is the day plant i blooms. You want to assemble m bundles, and every bundle needs k adjacent plants in the row that have all already bloomed (each plant can only go into one bundle). Return the earliest day on which all m bundles can be assembled, or -1 if it's never possible.
If m × k exceeds the number of plants, there simply aren't enough plants to ever form m bundles, regardless of how long you wait. Otherwise, waiting longer can only help: every plant bloomed by an earlier day stays bloomed on every later day, so the number of bundles achievable on a given day never decreases as the day advances. That monotonic "more days, never fewer bundles" relationship is exactly what a binary search on the answerBinary Search on the AnswerThe search runs directly over the space of candidate answers — here, every candidate day — rather than over an array. It applies whenever "is this candidate good enough?" only ever gets easier to satisfy as the candidate grows (or only harder, depending on direction). needs: search the range of days directly for the earliest one that already works.
Example 1:
Input: bloomDay = [3,5,1,9,4], m = 2, k = 2
Output: 9
Example 2:
Input: bloomDay = [9,9,9], m = 4, k = 2
Output: -1
Example 3:
Input: bloomDay = [6,6,6,6], m = 2, k = 2
Output: 6
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ number of plants ≤ 10⁵ - ●
1 ≤ bloomDay[i] ≤ 10⁹ - ●
1 ≤ m, k ≤ 10⁵
bloomDay =
[3, 5, 1, 9, 4]
m =
2
k =
2