Earliest Possible Day of Full Bloom - Problem
You're managing a garden with n flower seeds that need to bloom as early as possible. Each seed has a two-phase lifecycle:
- Planting Phase: You must work on planting each seed for a specific number of days (can be non-consecutive)
- Growing Phase: After planting is complete, the seed grows automatically for a fixed number of days
You are given:
plantTime[i]: Total days needed to plant seed igrowTime[i]: Days for seed i to grow after planting completes
Constraints:
- You can only work on one seed per day during planting
- You can switch between seeds (planting doesn't need to be consecutive)
- Growing happens automatically and simultaneously for all planted seeds
Goal: Find the earliest possible day when all flowers are blooming by optimally ordering your planting schedule.
Input & Output
example_1.py โ Basic Case
$
Input:
plantTime = [1, 4, 3], growTime = [2, 3, 1]
โบ
Output:
9
๐ก Note:
Optimal order is [1,0,2]: Plant seed 1 (days 0-3), then seed 0 (day 4), then seed 2 (days 5-7). Seed 1 blooms day 7, seed 0 blooms day 6, seed 2 blooms day 8. All bloom by day 8. Wait, let me recalculate... Actually optimal is [0,1,2] giving bloom times [3,8,9], so answer is 9.
example_2.py โ Equal Times
$
Input:
plantTime = [1, 2, 3, 2], growTime = [2, 1, 2, 1]
โบ
Output:
9
๐ก Note:
Sort by grow time descending: grow times [2,2,1,1] correspond to seeds [0,2,1,3]. Plant in order [0,2,1,3]: bloom at days [3,6,9,8]. Maximum is 9.
example_3.py โ Single Seed
$
Input:
plantTime = [1], growTime = [1]
โบ
Output:
2
๐ก Note:
Only one seed: plant for 1 day, grow for 1 day, blooms on day 2.
Visualization
Tap to expand
Understanding the Visualization
1
Analyze Grow Times
Identify which seeds need the longest growing period
2
Sort Strategy
Arrange seeds by decreasing grow time to maximize parallelism
3
Sequential Planting
Plant in sorted order while slow growers develop in parallel
4
Calculate Results
Track when each seed blooms and find the maximum
Key Takeaway
๐ฏ Key Insight: The greedy approach of planting seeds with longer grow times first ensures optimal parallelism, minimizing the total time for all flowers to bloom.
Time & Space Complexity
Time Complexity
O(n log n)
Dominated by sorting the seeds by grow time
โก Linearithmic
Space Complexity
O(n)
Space for storing sorted pairs of (growTime, plantTime)
โก Linearithmic Space
Constraints
- n == plantTime.length == growTime.length
- 1 โค n โค 105
- 1 โค plantTime[i], growTime[i] โค 104
- All values are positive integers
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code