Sponsored
Sponsored
In this approach, we prioritize seeds with shorter planting times first. The idea is that we can start planting these seeds earlier, and consequently, they will finish blooming earlier. This might minimize the delay caused by longer-growing seeds.
Time Complexity: O(n log n) due to sorting. Space Complexity: O(n) for storing seeds in an array.
1import java.util.*;
2
3class Seed {
4 int plantTime;
5 int growTime;
6
7 Seed(int plantTime, int growTime) {
8 this.plantTime = plantTime;
9 this.growTime = growTime;
10 }
11}
12
13public class Main {
14 public static int earliestFullBloom(int[] plantTime, int[] growTime) {
15 int n = plantTime.length;
16 Seed[] seeds = new Seed[n];
17
18 for (int i = 0; i < n; ++i) {
19 seeds[i] = new Seed(plantTime[i], growTime[i]);
20 }
21
22 Arrays.sort(seeds, (a, b) -> a.plantTime - b.plantTime);
23
24 int currentDay = 0;
25 int lastBloomDay = 0;
26
27 for (Seed seed : seeds) {
28 currentDay += seed.plantTime;
29 lastBloomDay = Math.max(lastBloomDay, currentDay + seed.growTime);
30 }
31
32 return lastBloomDay;
33 }
34
35 public static void main(String[] args) {
36 int[] plantTime = {1, 4, 3};
37 int[] growTime = {2, 3, 1};
38 System.out.println("Earliest Full Bloom Day: " + earliestFullBloom(plantTime, growTime));
39 }
40}
41
The program creates a Seed class for handling plant and grow times, sorts them, and tracks growing progress to determine when all seeds bloom.
This strategy focuses on seeds with longer grow times first. By doing so, we minimize delays caused by excessively long growing periods by ensuring they finish growing as soon as possible.
Time Complexity: O(n log n) because of sorting operations. Space Complexity: O(n) for storing seed data.
1
The Python solution creates a zip object and sorts it on grow times. It represents a computationally simple simulation of the day-to-day planting strategy.