Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Greedy/Earliest Possible Day of Full Bloom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,46 @@ class Solution {

}
};

/***************************************************************** JAVA ***********************************************************************************/

class Solution {
public int earliestFullBloom(int[] plantTime, int[] growTime) {
int n = plantTime.length;

// Create a list of pairs where each pair contains plantTime and growTime
List<Pair> vec = new ArrayList<>();
for (int i = 0; i < n; i++) {
vec.add(new Pair(plantTime[i], growTime[i]));
}

// Sort the pairs based on growTime in descending order
Collections.sort(vec, (P1, P2) -> P2.growTime - P1.growTime);

int prevPlantDays = 0;
int maxBloomDays = 0;
for (int i = 0; i < n; i++) {
int currPlantTime = vec.get(i).plantTime;
int currGrowTime = vec.get(i).growTime;

prevPlantDays += currPlantTime;

int currPlantBloomTime = prevPlantDays + currGrowTime;

maxBloomDays = Math.max(maxBloomDays, currPlantBloomTime);
}

return maxBloomDays;
}
}

// Helper class to store the pairs of plantTime and growTime
class Pair {
int plantTime;
int growTime;

Pair(int plantTime, int growTime) {
this.plantTime = plantTime;
this.growTime = growTime;
}
}