Minimum Number of Days to Make m Bouquets
Find the first day on which m bouquets of k neighbouring flowers can be made, by binary searching over the days.
A garden has nibloomDay[i]km
Example 1
Output: 3
Explanation: by day 3, the flowers that bloom on days 1, 3 and 2 are open: three bouquets of one flower.
Example 2
Output: -1
Explanation: 3 bouquets of 2 flowers need 6 flowers, and there are only 5.
Example 3
Output: 12
Explanation: on day 7 the flower that blooms on day 12 is still closed and splits the row, so only one bouquet fits. On day 12 every flower is open and two bouquets fit.
The Idea
Picture the seven plants of Example 3 along a garden path. Six of them bloom on day 7, and the fifth one blooms on day 12. You want 2 bouquets of 3 flowers each, and the 3 flowers of a bouquet must stand side by side. On day 7, six flowers are open, which sounds like exactly enough. But are they side by side?
Look at the picture below. In the row labelled day 7, the closed flower with bloom day 12 sits in the middle of the path. The four open flowers to its left give bouquet 1, with one flower left over, and the two open flowers to its right are one short of a bouquet. So day 7 gives only 1 bouquet.
In the row labelled day 12, the flower with bloom day 12 is open too. Together with the two flowers after it, it makes bouquet 2. So the answer is 12, and the question is how to find such a day in general.
Checking one day is easy: walk along the row and count the bouquets. Checking every day from the first bloom to the last can be slow, though. If the last flower blooms on day 1,000,000,000, that could mean a billion walks along the row.
Now look at the bottom row of the picture, labelled "2 bouquets by this day?". For days 7 to 11 the answer is no, and for day 12 it is yes. A flower that has opened stays open, so a later day never has fewer open flowers, and never fewer bouquets. Once the answer turns to yes, it stays yes for every later day.
That shape lets us skip most days. Try a day in the middle of the range. A yes means the first yes is that day or earlier, so the later days can be dropped. A no means the first yes comes later, so that day and the earlier ones can be dropped. Each check halves the days still in play. This halving is binary search, run here over days rather than over the positions of a sorted array.
Solution Approach
Turning that idea into code means settling four things: when the answer is -1, how to count the bouquets on one day, which days to search, and how to halve the range without skipping the first yes.
When no day is enough
Can waiting ever fail, when every flower blooms in the end? Yes: mkm × k
If the garden has enough flowers, the last bloom day always works: every flower is open, nothing breaks the row, and the mm × kintintlong
Counting bouquets on one day
How do we count the bouquets for a given day? A flower is open when bloomDay[i] <= dayrunrunkrunrun
Taking a bouquet as soon as a run reaches kk
The range of days to search
Which days can be the answer? Before the earliest bloom day nothing is open, so no bouquet can be made. On the latest bloom day everything is open, and that day works whenever the garden has enough flowers. So the search runs from the smallest value in bloomDaylohi
Halving the range
How do we halve the range without skipping the first yes? Keep the answer always between lohimidmidmmidhi = midmidlo = mid + 1lohi
In Example 3, lohilolohi
The middle day is written lo + (hi - lo) / 2(lo + hi) / 2int
Java Solution
The method minDays
- First, check the number of flowers: if
is more than(long) m * k , return -1.bloomDay.length - Find the range with one loop over
:bloomDay becomes the earliest bloom day andlo the latest.hi - While
is less thanlo , take the middle dayhi and ask the helper methodmid how many bouquets fit on that day. If the count is at leastbouquetsOnDay , setm ; otherwise sethi = mid .lo = mid + 1 - Inside
, a loop walks the row once with two counters:bouquetsOnDay for open flowers in a row andrun for bouquets made. A full run adds a bouquet and resetsbouquets , and a closed flower resets it too.run - When
andlo meet, returnhi , the first day with enough bouquets.lo
The mainm × kint
public class MinimumDaysForBouquets {
public static int minDays(int[] bloomDay, int m, int k) {
if ((long) m * k > bloomDay.length) {
return -1; // not enough flowers, whatever the day
}
int lo = Integer.MAX_VALUE;
int hi = 0;
for (int d : bloomDay) {
lo = Math.min(lo, d); // nothing blooms earlier
hi = Math.max(hi, d); // everything has bloomed by then
}
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (bouquetsOnDay(bloomDay, mid, k) >= m) {
hi = mid; // mid works: the answer is mid or earlier
} else {
lo = mid + 1; // mid is too early
}
}
return lo;
}
// Count bouquets of k adjacent bloomed flowers that can be made on 'day'
static int bouquetsOnDay(int[] bloomDay, int day, int k) {
int bouquets = 0;
int run = 0; // bloomed flowers in a row so far
for (int d : bloomDay) {
if (d <= day) {
run++;
if (run == k) {
bouquets++;
run = 0; // these k flowers are used up
}
} else {
run = 0; // a flower that has not bloomed breaks the run
}
}
return bouquets;
}
public static void main(String[] args) {
System.out.println(minDays(new int[] {1, 10, 3, 10, 2}, 3, 1));
System.out.println(minDays(new int[] {1, 10, 3, 10, 2}, 3, 2));
int[] garden = {7, 7, 7, 7, 12, 7, 7};
System.out.println(minDays(garden, 2, 3));
System.out.println("day 7: " + bouquetsOnDay(garden, 7, 3)
+ ", day 11: " + bouquetsOnDay(garden, 11, 3)
+ ", day 12: " + bouquetsOnDay(garden, 12, 3));
System.out.println(minDays(new int[] {5, 1, 3}, 1, 3));
System.out.println(minDays(new int[] {1, 10, 3, 10, 2}, 65536, 65536));
}
}
Running it prints:
3
-1
12
day 7: 1, day 11: 1, day 12: 2
5
-1
Examples 1 to 3 print 3, -1 and 12, and the -1 comes from the flower count alone, before any day is checked. The fourth line backs up the picture. Example 3 makes 1 bouquet on day 7, still 1 on day 11, the last day the search checked, and 2 on day 12.
The fifth line comes from [5, 1, 3]m = 1k = 3hi
The last line, -1, is the product check. 65,536 × 65,536 is 4,294,967,296, and multiplied as an intintlong
Complexity
The work is one pass to find the range, then one walk along the row for every day the search checks.
- Time: O(n log D), where D is the number of days from the earliest bloom day to the latest. Each check halves the days still in play, so there are about log2 D checks, and each walks all n flowers.
- Space: O(1). The search keeps
,lo andhi , the walk keepsmid andrun , and the array is never copied.bouquets