Earliest Possible Day of Full Bloom - Problem

You have n flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed.

You are given two 0-indexed integer arrays plantTime and growTime, of length n each:

  • plantTime[i] is the number of full days it takes you to plant the i-th seed. Every day, you can work on planting exactly one seed. You do not have to work on planting the same seed on consecutive days, but the planting of a seed is not complete until you have worked plantTime[i] days on planting it in total.
  • growTime[i] is the number of full days it takes the i-th seed to grow after being completely planted. After the last day of its growth, the flower blooms and stays bloomed forever.

From the beginning of day 0, you can plant the seeds in any order.

Return the earliest possible day where all seeds are blooming.

Input & Output

Example 1 — Basic Case
$ Input: plantTime = [1,4,3], growTime = [2,3,1]
Output: 9
💡 Note: Optimal order is to plant seed 1 (grow time 3), then seed 0 (grow time 2), then seed 2 (grow time 1). Plant seed 1 on days 0-3 (completes day 4), blooms day 7. Plant seed 0 on day 4 (completes day 5), blooms day 7. Plant seed 2 on days 5-7 (completes day 8), blooms day 9. All seeds bloom by day 9.
Example 2 — Equal Plant Times
$ Input: plantTime = [1,2,3,2], growTime = [2,1,2,1]
Output: 9
💡 Note: Sort by grow time descending: seeds 0,2 (grow=2), then seeds 1,3 (grow=1). Plant in order [0,2,1,3]. Seed 0: plant days 0, blooms day 1+2=3. Seed 2: plant days 1-3, blooms day 4+2=6. Seed 1: plant days 4-5, blooms day 6+1=7. Seed 3: plant days 6-7, blooms day 8+1=9.
Example 3 — Single Seed
$ Input: plantTime = [1], growTime = [1]
Output: 2
💡 Note: Only one seed: plant it on day 0, completes day 1, blooms on day 1+1=2.

Constraints

  • n == plantTime.length == growTime.length
  • 1 ≤ n ≤ 105
  • 1 ≤ plantTime[i], growTime[i] ≤ 104

Visualization

Tap to expand
Earliest Possible Day of Full Bloom INPUT 3 Flower Seeds 0 1 2 plantTime[] 1 4 3 [0] [1] [2] growTime[] 2 3 1 [0] [1] [2] Plant one seed at a time Growth starts after planting Find earliest bloom day for ALL flowers ALGORITHM STEPS 1 Sort by growTime Descending order (longest first) 2 Sorted Order Seed 1 (g=3), 0 (g=2), 2 (g=1) 3 Plant Sequentially Track current day + bloom 4 Track Max Bloom Answer = max bloom day Timeline: 0 1 2 3 4 5 6 7 8 9 Plant 1 Grow P0 Grow Plant 2 G Red dots = bloom day FINAL RESULT Seed 1 Day 7 Seed 0 Day 7 Seed 2 Day 9 Output 9 All flowers bloom by Day 9 max(7, 7, 9) = 9 days Key Insight: Sort seeds by growTime in DESCENDING order (greedy approach). Seeds with longer growth times should be planted first, so they can grow while we plant other seeds. This minimizes the total time until all flowers bloom by overlapping planting and growing phases. TutorialsPoint - Earliest Possible Day of Full Bloom | Greedy: Sort by Grow Time
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~25 min Avg. Time
845 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