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 <iostream>
2#include <vector>
3#include <algorithm>
4
5using namespace std;
6
7struct Seed {
8 int plantTime;
9 int growTime;
10};
11
12bool compare(const Seed& a, const Seed& b) {
13 return a.plantTime < b.plantTime;
14}
15
16int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
17 int n = plantTime.size();
18 vector<Seed> seeds(n);
19 for (int i = 0; i < n; ++i) {
20 seeds[i] = {plantTime[i], growTime[i]};
21 }
22 sort(seeds.begin(), seeds.end(), compare);
23
24 int currentDay = 0;
25 int lastBloomDay = 0;
26 for (const auto& seed : seeds) {
27 currentDay += seed.plantTime;
28 lastBloomDay = max(lastBloomDay, currentDay + seed.growTime);
29 }
30 return lastBloomDay;
31}
32
33int main() {
34 vector<int> plantTime = {1, 4, 3};
35 vector<int> growTime = {2, 3, 1};
36 cout << "Earliest Full Bloom Day: " << earliestFullBloom(plantTime, growTime) << endl;
37 return 0;
38}
39
The solution uses structs to organize planting and growth times, sorts the seeds by plant time, and simulates planting to track the maximum bloom day.
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
JavaScript’s strategy involves mapping and sorting seeds according to their grow times. The implementation calculates cumulative days for blooming in an ordered manner.