You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 3 Solutions 🔥 || Simple Fast and Easy || with Explanation
Solution - 1 Sorting
classPair {
lateint growTime, plantTime;
Pair(int growTime, int plantTime) {
this.growTime = growTime;
this.plantTime = plantTime;
}
}
classSolution {
intearliestFullBloom(List<int> plantTime, List<int> growTime) {
List<Pair> flowers = [];
int flowerCount = plantTime.length;
for (int i =0; i < flowerCount; i++)
flowers.add( Pair(growTime[i], plantTime[i]));
//=========================================================//Sort the flowers on basis of decreasing growTime
flowers.sort((Pair a, Pair b) {
if (a.growTime > b.growTime)
return-1;
elseif (a.growTime < b.growTime)
return1;
elsereturn0;
});
//========================================================//currentTime denotes the time at which I finished planting the current flower//After the currentTime I am now free to plant another plant while the current flower grows// SAVE TIME :)int currentTime =0, mxBloomTime =0;
for (Pair flower in flowers) {
currentTime += flower.plantTime;
int bloomTime = currentTime +
flower.growTime; //current flower will bloom at bloomTime
mxBloomTime =max(mxBloomTime, bloomTime);
}
//=======================================================return mxBloomTime;
}
}
Solution - 2 Greedy
GREEDY LOGIC
We can only save time while the flower is "Growing". We are not occupied while some flower is growing
So to save time
Pick the flower with highest growing time at each step
More growing time means more free time with us
More free time means we can occupy ourselves in planting some other flower
classSolution {
intearliestFullBloom(List<int> plantTime, List<int> growTime) {
// sort by growTimeint n = plantTime.length;
List<int> arr =List.filled(100001, 0);
for (int i =0; i < n; i++) {
arr[growTime[i]] += plantTime[i];
}
int current =0, ans =0;
for (int i = arr.length -1; i >=1; i--) {
if (arr[i] ==0) continue;
current += arr[i];
ans =max(ans, current + i);
}
return ans;
}
}
Solution - 3 2D Array
classSolution {
intearliestFullBloom(List<int> plantTime, List<int> growTime) {
int n = plantTime.length;
List<List<int>> pairs =List.filled(n, 0).map((e) =>List.filled(2, 0)).toList();
for (int i =0; i < n; i++) {
pairs[i][0] = plantTime[i];
pairs[i][1] = growTime[i];
}
pairs.sort((a, b) => b[1] - a[1]);
int plantingDays =0;
int totalDays =0;
for (List<int> pair in pairs) {
totalDays =max(totalDays, plantingDays + pair[0] + pair[1]);
plantingDays += pair[0];
}
return totalDays;
}
}