Pattern visualizer
Minimum Days to Make M Bouquets
The array is not sorted and sorting it would destroy the adjacency the bouquets depend on, so there is nothing here to binary search directly. What IS ordered is the calendar: a flower open on day 9 is still open on day 10, so the number of bouquets available never falls as the day rises. Feasibility flips from no to yes exactly once along that line, and binary search hunts that flip. Each guessed day is tested by one left-to-right walk that counts the current run of open flowers, cutting a bouquet every time the run reaches k and resetting the run at any flower still in bud. Animated on: bloomDay = [7,7,7,7,13,11,12,7], m = 2, k = 3 — each bouquet needs 3 ADJACENT flowers that have already bloomed; find the earliest day 2 bouquets can be made..
Binary search the day, scan adjacent runs to test it
Each cell is the day that flower opens. We need 2 bouquets of 3 ADJACENT flowers each, and picking one never blocks another, so the only question is which day to harvest. Nothing opens before day 7 and everything is open by day 13, so the answer lives in [7, 13].
1FUNCTION minDays(bloom, m, k):2 IF m * k > LENGTH(bloom):3 RETURN -14 low <- MIN(bloom)5 high <- MAX(bloom)6 WHILE low < high:7 mid <- (low + high) / 28 IF BOUQUETS(bloom, mid, k) >= m:9 high <- mid10 ELSE:11 low <- mid + 112 RETURN low
← / → step · space play · Home restart