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.
1#include <vector>
#include <algorithm>
using namespace std;
struct Seed {
int plantTime;
int growTime;
};
bool compare(const Seed& a, const Seed& b) {
return a.growTime > b.growTime;
}
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
int n = plantTime.size();
vector<Seed> seeds(n);
for (int i = 0; i < n; ++i) {
seeds[i] = {plantTime[i], growTime[i]};
}
sort(seeds.begin(), seeds.end(), compare);
int totalPlantDays = 0;
int maxBloomDay = 0;
for (const auto& seed : seeds) {
totalPlantDays += seed.plantTime;
maxBloomDay = max(maxBloomDay, totalPlantDays + seed.growTime);
}
return maxBloomDay;
}
int main() {
vector<int> plantTime = {1, 4, 3};
vector<int> growTime = {2, 3, 1};
cout << "Earliest Full Bloom Day: " << earliestFullBloom(plantTime, growTime) << endl;
return 0;
}
This solution uses C++ structs to hold seed data, sorts based on grow time (descending order), and tracks concurrent activities, allowing delayed blooms to conclude sooner.