Member-only story
Leetcode 1482: Minimum Number of Days to Make m Bouquets
3 min readJun 26, 2020
In this Leetcode problem, we want to find out after how many days we need to make m
bouquets with k
flowers with some particular rules:
Solution
A quick note that my solution is very slow, but still passing the submission test. Definitely not optimal but simple to understand.
Here are the couple of ideas I used to resolve the problem:
- Sort the flowers by the days they will bloom in
count
that maps the days of blooming to the list of flowers blooming on that day. - Use flower
Interval
to store the current state of bloomed flowers. - Map the intervals by first flower in
start
and by last flower inend
. - When adding a new flower there are 4 cases: create a new interval, append to the right of an interval, append to the left of an interval, or merge 2 intervals if the flower added is making 2 intervals join.
- While we do that, we count the maintain the max number of bouquets we can have at each step. When
bouquet >= m
we return the current day.
Here is the full code:
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
Map<Integer, List<Integer>> count = new TreeMap<>();
int p = m * k…