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 ith
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 ith
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.
Example 1:
Input: plantTime = [1,4,3], growTime = [2,3,1] Output: 9 Explanation: The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms. One optimal way is: On day 0, plant the 0th seed. The seed grows for 2 full days and blooms on day 3. On days 1, 2, 3, and 4, plant the 1st seed. The seed grows for 3 full days and blooms on day 8. On days 5, 6, and 7, plant the 2nd seed. The seed grows for 1 full day and blooms on day 9. Thus, on day 9, all the seeds are blooming.
Example 2:
Input: plantTime = [1,2,3,2], growTime = [2,1,2,1] Output: 9 Explanation: The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms. One optimal way is: On day 1, plant the 0th seed. The seed grows for 2 full days and blooms on day 4. On days 0 and 3, plant the 1st seed. The seed grows for 1 full day and blooms on day 5. On days 2, 4, and 5, plant the 2nd seed. The seed grows for 2 full days and blooms on day 8. On days 6 and 7, plant the 3rd seed. The seed grows for 1 full day and blooms on day 9. Thus, on day 9, all the seeds are blooming.
Example 3:
Input: plantTime = [1], growTime = [1] Output: 2 Explanation: On day 0, plant the 0th seed. The seed grows for 1 full day and blooms on day 2. Thus, on day 2, all the seeds are blooming.
Constraints:
n == plantTime.length == growTime.length
1 <= n <= 105
1 <= plantTime[i], growTime[i] <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach focuses on trying every possible day as the bloom day and checking if all the flowers can bloom by that day, then finding the earliest of such days. It's like trying every single date on the calendar until we find one that works.
Here's how the algorithm would work step-by-step:
def earliest_full_bloom_brute_force(plant_time, grow_time):
number_of_flowers = len(plant_time)
possible_bloom_days = []
# Iterate through all possible days to find the earliest bloom day
for current_day in range(1, 100001):
all_bloomed = True
# Check if all flowers have bloomed by the current day
for flower_index in range(number_of_flowers):
bloom_day = plant_time[flower_index] + grow_time[flower_index]
if current_day < plant_time[flower_index] + grow_time[flower_index]:
all_bloomed = False
break
# If all flowers have bloomed, store the current day
if all_bloomed:
possible_bloom_days.append(current_day)
# Find the earliest bloom day among all possibilities
if possible_bloom_days:
earliest_day = min(possible_bloom_days)
return earliest_day
else:
return -1
To find the earliest day all flowers bloom, we need to figure out the best planting order. The key is to prioritize planting flowers that take longer to grow earlier in the schedule, so their bloom time doesn't delay the entire process.
Here's how the algorithm would work step-by-step:
def earliest_full_bloom(plant_time, bloom_time):
number_of_flowers = len(plant_time)
flower_data = []
for i in range(number_of_flowers):
flower_data.append((plant_time[i], bloom_time[i]))
# Sort flowers by bloom time in descending order
flower_data.sort(key=lambda x: x[1], reverse=True)
current_planting_day = 0
latest_bloom_day = 0
for planting_time, blooming_time in flower_data:
# Update current planting day after previous plant
current_planting_day += planting_time
#Determine bloom day and update the latest bloom
bloom_day = current_planting_day + blooming_time
latest_bloom_day = max(latest_bloom_day, bloom_day)
return latest_bloom_day
Case | How to Handle |
---|---|
Empty `plantTime` or `growTime` array | Return 0 if either array is empty, as no plants can be planted. |
`plantTime` and `growTime` arrays have different lengths | Throw an IllegalArgumentException since the arrays must have corresponding elements. |
All `plantTime` or `growTime` values are 0 | The blooming day will be the sum of the plant times (if `growTime` is all 0) or max of grow times (if `plantTime` is all 0), so the algorithm should work correctly. |
Large arrays (e.g., length of `plantTime` and `growTime` is 10^5) | Ensure the sorting algorithm used has O(n log n) time complexity to avoid timeouts. |
`growTime` values are in descending order | The sorting step based on growTime will reverse this and maximize parallelization benefit, which is intended. |
Negative values in `plantTime` or `growTime` | Throw an IllegalArgumentException, as plant and grow times cannot be negative. |
Integer overflow when calculating the final bloom day | Use `long` data type to prevent integer overflow when calculating cumulative planting time and bloom day. |
`plantTime` and `growTime` have maximum integer values | Using `long` during the calculations should prevent integer overflow, ensuring the answer is correct. |