Earliest Possible Day of Full Bloom

Updated on 23 May, 2025
Earliest Possible Day of Full Bloom header image

Problem Statement

In this problem, we are given n flower seeds, and our task is to determine the minimum number of days required until all seeds are blooming. Each seed requires a specific amount of time to plant and a separate amount of time to grow, after which it blooms permanently.

You are provided two arrays:

  • plantTime, where plantTime[i] represents the number of full days required to plant the i-th seed.
  • growTime, where growTime[i] represents the days a seed needs to grow after planting is complete.

The progress of planting a seed can be spread out over non-consecutive days, but a seed only starts growing after its planting has been fully completed. You have complete flexibility in choosing the order of planting the seeds.

The goal is to determine the earliest possible day where all seeds are blooming, given that you start working from day 0.

Examples

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

Approach and Intuition

To tackle this problem effectively, understanding the relationship between planting and growing times is crucial. Let's break down the problem with a strategy:

  1. Each seed has its own growing time which ticks only after its respective planting time is complete.
  2. It is beneficial from an optimization perspective to prioritize the planting of seeds which have a longer growth phase (growTime). This is because, once planted, their longer growth durations will have a significant impact on the completion date of all seeds blooming.
  3. The strategy involves sorting the seeds primarily based on their growing times in descending order. So seeds with longer growth times should be planted as early as possible.
  4. Track the currentDay as we proceed to plant each seed.
  5. For each seed:
    • Add the planting time of the currently selected seed to the currentDay.
    • Compute the day the seed will bloom by considering the end of its growing time from the day planting was completed.
    • Update the latestBloom as the maximum of its current value or the bloom day of the current seed.
  6. The end result, latestBloom, represents the earliest day when all seeds are guaranteed to be blooming.

In essence, by prioritizing planting of those seeds that take longer to bloom post planting, we can optimize for the earliest day all flowers will bloom. This optimal planting strategy ensures that while we plant one seed, another can simultaneously go through its growth phase efficiently. This method ideally leverages simultaneous progression of varied growth times among the seeds.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int findOptimalBloomTime(vector<int>& plantingTimes, vector<int>& growingTimes) {
        vector<int> idxes(plantingTimes.size());
        iota(idxes.begin(), idxes.end(), 0);
        sort(idxes.begin(), idxes.end(),
             [&](int i, int j) { return growingTimes[i] > growingTimes[j]; });
        int maxBloomTime = 0, currentPlantingTime = 0;
        for (int idx : idxes) {
            currentPlantingTime += plantingTimes[idx];
            maxBloomTime = max(maxBloomTime, currentPlantingTime + growingTimes[idx]);
        }
        return maxBloomTime;
    }
};

This C++ solution involves determining the earliest possible day all plants can be fully in bloom for a given set of planting and growing times. The solution approach focuses on optimizing the planting order based on the growing times to minimize the total bloom completion time. Follow these steps to comprehend the method utilized:

  1. Create a vector of indices corresponding to the plants. Initialize this vector with sequential numbers starting from zero, which represent each plant.
  2. Sort the indices based on the growing times of each plant in descending order. This is done using a custom comparator within the sort function that compares the growing times of two plants.
  3. Initialize maxBloomTime and currentPlantingTime to zero. maxBloomTime tracks the latest day by which all plants have bloomed, and currentPlantingTime tracks the cumulative time spent on planting up to each plant.
  4. Iterate through the sorted list of indices:
  • For each plant, increment the currentPlantingTime by the planting time of the current plant.
  • Update the maxBloomTime to ensure it is the maximum value between itself and the sum of currentPlantingTime and the growing time of the current plant.
  1. Return maxBloomTime, which will be the earliest day all plants are fully bloomed under the optimal planting strategy.

The process effectively lines up longer-growing plants to be planted earlier, allowing them more time to grow while other plants are being planted, ultimately optimizing the full bloom day.

java
class Solution {
    public int calculateMinimumDays(int[] seedingTime, int[] developmentTime) {
        int totalPlants = developmentTime.length;
        ArrayList<Integer> sortedIndices = new ArrayList<>();
        for (int i = 0; i < totalPlants; i++) {
            sortedIndices.add(i);
        }
        Collections.sort(sortedIndices, (i, j) -> developmentTime[j] - developmentTime[i]);
        int finalDay = 0;
        int currentStartDay = 0;
        for (int i = 0; i < totalPlants; i++) {
            int index = sortedIndices.get(i);
            int dayNeeded = currentStartDay + seedingTime[index] + developmentTime[index];
            finalDay = Math.max(finalDay, dayNeeded);
            currentStartDay += seedingTime[index];
        }
        return finalDay;
    }
}

The Java solution provided addresses the problem of calculating the earliest possible day for all plants to fully bloom given their specific seeding and development times. Here’s a breakdown of how this solution operates:

  • First, initialize an integer array seedingTime and developmentTime, with both arrays containing respective times for each plant.
  • Determine the total number of plants using developmentTime.length.
  • Create an ArrayList<Integer> named sortedIndices to store the indices of the plants. These indices will be sorted based on the development time in descending order, ensuring plants with longer development times are prioritized.
  • Sort sortedIndices using a custom comparator that compares the development times of two indices in the descending order.
  • Initialize two variables, finalDay and currentStartDay, to 0. finalDay tracks the maximum number of days required for all plants to bloom, while currentStartDay tracks the cumulative days as you plant each seed.
  • Iterate through the sorted list of plant indices. For each plant:
    • Calculate the total days required (dayNeeded) by adding the current start day, the seeding time, and the development time of the current plant.
    • Update finalDay to be the maximum of its current value and dayNeeded.
    • Increment currentStartDay by the seeding time of the current plant to account for the time moved forward due to planting.
  • On completion of the loop, the method returns finalDay, which will be the earliest possible day when all plants can be in full bloom.

By strategically ordering the planting process—starting with plants that take the longest to develop and continuously updating the start day—the algorithm efficiently calculates the minimal day required for full bloom. This solution ensures optimal planting scheduling for minimal total bloom time, leveraging sorting and careful iteration through the plant indices.

python
class Solution:
    def maxBloomDay(self, plantingDays: List[int], growingDays: List[int]) -> int:
        currentDay = 0
        maxDay = 0
        order = sorted(range(len(plantingDays)), key=lambda i: -growingDays[i])
        for idx in order:
            currentDay += plantingDays[idx]
            maxDay = max(maxDay, currentDay + growingDays[idx])
        return maxDay

The problem "Earliest Possible Day of Full Bloom" is resolved in Python by calculating when all given plants can be in full bloom. The provided solution involves processing lists that document both the days required to plant each flower and the days needed for each to grow to full bloom.

The solution involves these steps:

  1. Initialize variables to track the current day and the latest day any plant achieves full bloom.
  2. Create an order for planting the flowers based on which ones take the longest to grow, reasoning that planting the slower-growing plants first can optimize the total growing period.
  3. Iterate over the sorted flowers, updating the currentDay continually by adding the planting days for each ordered flower.
  4. After planting each flower, calculate the full bloom day by adding the growing days and compare it with maxDay to keep track of the most delayed bloom.
  5. Return the value of maxDay, which will provide the earliest day by which all flowers are in full bloom if followed by the optimized planting strategy.

This approach effectively finds the optimal day to have all the flowers blooming by prioritizing the planting of slower-growing flowers and scheduling the gardening sequence efficiently.

Comments

No comments yet.