#2136
Earliest Possible Day of Full Bloom
HardArrayGreedySortingGreedySorting
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n!) | O(n log n) |
| Space | O(n) | O(n) |
💡
Intuition
Time O(n log n)Space O(n)
The optimal solution involves sorting the seeds based on their grow time in descending order. This way, we prioritize planting seeds that take longer to grow first, minimizing the overall bloom time.
⚙️
Algorithm
5 steps- 1Step 1: Create a list of tuples containing plant and grow times.
- 2Step 2: Sort this list by grow time in descending order.
- 3Step 3: Initialize variables to track the current day and the latest bloom day.
- 4Step 4: Iterate through the sorted list, updating the current day and calculating the bloom day for each seed.
- 5Step 5: Return the maximum bloom day.
solution.py8 lines
1def earliestBloomDay(plantTime, growTime):
2 seeds = sorted(zip(plantTime, growTime), key=lambda x: -x[1])
3 current_day = 0
4 max_bloom_day = 0
5 for p, g in seeds:
6 current_day += p
7 max_bloom_day = max(max_bloom_day, current_day + g)
8 return max_bloom_dayℹ
Complexity note: The time complexity is O(n log n) due to the sorting step, while the space complexity is O(n) for storing the seeds in a list.
- 1Prioritize planting seeds with longer grow times first to minimize overall bloom time.
- 2Sorting based on grow time allows for an efficient planting strategy.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.