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.
1#include <stdio.h>
2#include <stdlib.h>
3
4struct Seed {
5 int plantTime;
6 int growTime;
7};
8
9int compare(const void *a, const void *b) {
10 struct Seed *seedA = (struct Seed *)a;
11 struct Seed *seedB = (struct Seed *)b;
12 return seedA->plantTime - seedB->plantTime;
13}
14
15int earliestFullBloom(int* plantTime, int plantTimeSize, int* growTime, int growTimeSize) {
16 struct Seed seeds[plantTimeSize];
17 for (int i = 0; i < plantTimeSize; i++) {
18 seeds[i].plantTime = plantTime[i];
19 seeds[i].growTime = growTime[i];
20 }
21
22 qsort(seeds, plantTimeSize, sizeof(struct Seed), compare);
23
24 int currentDay = 0;
25 int lastBloomDay = 0;
26 for (int i = 0; i < plantTimeSize; i++) {
27 currentDay += seeds[i].plantTime;
28 int bloomDay = currentDay + seeds[i].growTime;
29 if (bloomDay > lastBloomDay) {
30 lastBloomDay = bloomDay;
31 }
32 }
33 return lastBloomDay;
34}
35
36int main() {
37 int plantTime[] = {1, 4, 3};
38 int growTime[] = {2, 3, 1};
39 int result = earliestFullBloom(plantTime, 3, growTime, 3);
40 printf("Earliest Full Bloom Day: %d\n", result);
41 return 0;
42}
43
This solution defines a structure for seeds, sorts them based on planting time, and calculates when all flowers will 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.
1using System;
2using 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.