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.
1function earliestFullBloom(plantTime, growTime) {
2 const seeds = plantTime.map((pt, idx) => ({
3 plantTime: pt,
4 growTime: growTime[idx]
5 }));
6
7 seeds.sort((a, b) => a.plantTime - b.plantTime);
8
9 let currentDay = 0;
10 let lastBloomDay = 0;
11
12 for (let i = 0; i < seeds.length; i++) {
13 currentDay += seeds[i].plantTime;
14 lastBloomDay = Math.max(lastBloomDay, currentDay + seeds[i].growTime);
15 }
16
17 return lastBloomDay;
18}
19
20const plantTime = [1, 4, 3];
21const growTime = [2, 3, 1];
22console.log("Earliest Full Bloom Day:", earliestFullBloom(plantTime, growTime));
23
JavaScript's solution utilizes sorting based on plant times and subsequently tracks the bloom days by employing the sort routine and cumulative day tracking.
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.
1using System.Linq;
class Seed {
public int PlantTime { get; set; }
public int GrowTime { get; set; }
}
class Program {
public static int EarliestFullBloom(int[] plantTime, int[] growTime) {
Seed[] seeds = new Seed[plantTime.Length];
for (int i = 0; i < plantTime.Length; i++) {
seeds[i] = new Seed { PlantTime = plantTime[i], GrowTime = growTime[i] };
}
Array.Sort(seeds, (a, b) => b.GrowTime.CompareTo(a.GrowTime));
int totalPlantDays = 0;
int maxBloomDay = 0;
foreach (var seed in seeds) {
totalPlantDays += seed.PlantTime;
maxBloomDay = Math.Max(maxBloomDay, totalPlantDays + seed.GrowTime);
}
return maxBloomDay;
}
static void Main() {
int[] plantTime = { 1, 4, 3 };
int[] growTime = { 2, 3, 1 };
Console.WriteLine("Earliest Full Bloom Day: " + EarliestFullBloom(plantTime, growTime));
}
}
The C# implementation uses the Seed class to store planting and growing times, sorts it by grow time, and tracks daily operations.