2136. Earliest Possible Day of Full Bloom

2136. Earliest Possible Day of Full Bloom

Problem Solving - Day 29

ยท

5 min read

Hello, reader ๐Ÿ‘‹๐Ÿฝ ! Welcome to day 29 of the series on Problem Solving. Through this series, I aim to pick up at least one question everyday and share my approach for solving it.

Today, I will be picking up LeetCode's daily challenge problem: 2136. Earliest Possible Day of Full Bloom.


๐Ÿค” Problem Statement

  • There are 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. There are two 0-indexed integer arrays plantTime and growTime, of length n each:
    • plantTime[i] is the number of full days it takes to plant the ith seed.
    • growthTime[i] is the number of full days it takes to plant ith seed.
  • Every day exactly one seed can be planted and planting the same seed need not be worked on consecutive days.
  • But planting of a seed is not complete until plantTime[i] days are worked on planting.
  • After the last day of a plant's growth, the flower blooms and stays bloomed forever.
  • Return the earliest possible day where all seeds are blooming.
  • E.g.:
    • plantTime = [1,4,3], growTime = [2,3,1] => 9
    • plantTime = [1,2,3,2], growTime = [2,1,2,1] => 9

๐Ÿ’ฌ Thought Process - Greedy

  • After reading the question it was kind of clear that dp was not the way to solve this.
  • Because it was stated in the question (and also explained in the test cases) that the order in which plants are planted doesn't matter.
  • At the end of the array, a plant will take the same time to plant and grow no matter when it was planted: 1st or last.
  • Hence the next question was some structure in the way to plant trees.
  • From the example test cases, it was also clear that a new tree cannot be planted until the previous tree's planting time was complete. Hence the waiting time to start planting a particular tree was the time taken to plant the previous tree.
  • So in no way, could we find a "shortcut" to start planting seeds earlier that it's actually possible. This waiting time was unavoidable.
  • Hence to minimise the overall bloom time, we have to focus on some way to spend as little time as possible during growth.
  • Another thing to observer is that while a plant is in the growth stage (i.e. once it has been planted), another plant could be in the seed planting process.
  • With this as the base idea, we can try to plant all the seeds that takes the maximum time growing first then focus on the seeds that take less time.

    Longer a plant takes to grow, sooner it needs to be planted

    • src: LeetCode.
  • Hence, we will sort seeds such that the plants that take larger time to grow are planted before those that take lesser time.

  • That's it. We are done.
  • Although, there's one edge case I'd like to point out.

    • Let's take the cases of two plants i and j. Let's assume plant i bloom on day s and plant j that was planted after i, blooms on day t.
      • But t < s. In cases like these, we would consider the bloom time to be the maximum of both which is s.
      • An e.g. for the following can be seen in this test case:
        • plantTime = [27,5,24,17,27,4,23,16,6,26,13,17,21,3,9,10,28,26,4,10,28,2]
        • growthTime = [26,9,14,17,6,14,23,24,11,6,27,14,13,1,15,5,12,15,23,27,28,12]
        • After sorting, we get the last two plants as following: [10, 5], [3, 1]. It can be seen (after sorting and planting the previous seeds that):
          • The plant [10, 5] is planted on day 333. It takes 10 days to complete working on it, which is 333 + 10 = 343. It then blooms 5 days later, which is: 343 + 5 = 348.
          • The plant [3, 1] is planted on day 343. It takes 3 days to complete working on it, which is: 343 + 3 = 346. It then blooms 1 day later, which is: 346 + 1 = 347.
          • Clearly the previous plant took more time to complete growing. So we will keep in mind to include the maximum number of days of growth time to ensure all the plants have bloomed.
  • Now, let's dive into the code.


๐Ÿ‘ฉ๐Ÿฝโ€๐Ÿ’ป Solution - Greedy

class Solution {
    public int earliestFullBloom(int[] plantTime, int[] growTime) {
        if(plantTime == null || plantTime.length == 0 || growTime == null || growTime.length == 0) {
            return 0;
        }

        int n = plantTime.length;
        int[][] plants = new int[n][2];

        for(int i = 0; i<n; i++) {
            plants[i] = new int[] {
                plantTime[i], growTime[i]
            };
        }

        Arrays.sort(plants, new CustomComparator());

        int pTime = 0, gTime = 0;
        for(int i = 0; i<n; i++) {
            pTime = pTime + plants[i][0];
            gTime = Math.max(gTime, pTime + plants[i][1]);
        }

        return gTime;
    }
}

class CustomComparator implements Comparator<int[]> {
    public int compare(int[] plant1, int[] plant2) {
        return plant2[1] - plant1[1];
    }
}

Time Complexity: O(n * logn)

- n = number of plants
    - Since we are sorting all the plants based on growth time.

Space Complexity: O(n)

- n = number of plants
    - Since we are combining the plantTime and growthTime into a separate data structure.

```



Conclusion

That's a wrap for today's problem. If you liked my explanation then please do drop a like/ comment. Also, please correct me if I've made any mistakes or if you want me to improve something!

Thank you for reading!