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:

  1. Planting Phase: You must work on planting each seed for a specific number of days (can be non-consecutive)
  2. 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 i
  • growTime[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
๐ŸŒป Garden Bloom Timeline OptimizationOptimal Strategy VisualizationExample: Plant=[1,4,3], Grow=[2,3,1]Step 1: Sort by grow time โ†’ [(3,4), (2,1), (1,3)]Timeline (days 0-9):Plant Seed 1 (4 days)Grow (3 days)โ†’ Blooms day 7P(1)Grow (2)โ†’ Blooms day 7Plant (3 days)G(1)โ†’ Blooms day 9Result: All flowers bloom by day 9!๐ŸŽฏ Key Insight:Plant longest-growing seeds first to maximize parallel development
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for storing sorted pairs of (growTime, plantTime)

n
2n
โšก Linearithmic Space

Constraints

  • n == plantTime.length == growTime.length
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค plantTime[i], growTime[i] โ‰ค 104
  • All values are positive integers
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen