Minimum Number of Days to Make m Bouquets
Master this topic with zero to advance depth.
Minimum Number of Days to Make m Bouquets
Return the minimum number of days such that you can make bouquets using adjacent flowers each. Each bouquet must consist of adjacent flowers.
Examples
Level I: Brute Force (Iterate Days)
Intuition
Try every possible day from to . For each day, check if it's possible to form bouquets.
Detailed Dry Run
bloomDay = [1,10,3,10,2], m=3, k=1. Sorted unique days: [1,2,3,10]. Day 1: 1 bouquet. Day 2: 2 bouquets. Day 3: 3 bouquets. Return 3.
Level II: Optimal (Optimization with Sorted Days)
Intuition
Instead of checking every day from 1 to maxD, only check days where at least one flower blooms. These are the unique values in bloomDay.
Detailed Dry Run
bloomDay = [1,10,3,10,2], m=3, k=1. Unique days sorted: [1, 2, 3, 10]. Check 1: 1 (fail), 2: 2 (fail), 3: 3 (pass). Result 3.
Level III: Optimal (Binary Search on Answer)
Intuition
The number of bouquets possible is a monotonic function of days. We can binary search the result in the range .
Detailed Dry Run
bloomDay = [1,10,3,10,2], m=3, k=1. Range [1, 10].
- Mid 5: [B, No, B, No, B] -> 3 bouquets. OK, R=5.
- Mid 2: [B, No, No, No, B] -> 2 bouquets. Too few, L=3.
- Mid 3: [B, No, B, No, B] -> 3 bouquets. OK, R=3. Result: 3.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.