Earliest Possible Day of Full Bloom LeetCode solution in JavaScript

1
/**
2
* @param {number[]} plantTime
3
* @param {number[]} growTime
4
* @return {number}
5
*/
6

7
var earliestFullBloom = function (plantTime, growTime) {
8
// create array of plant indices to identify plants after sorting
9
const plants = plantTime.map((v, i) => i);
10
// sort plants by longest grow time
11
const sortedByGrowTime = plants.sort((a, b) => growTime[b] - growTime[a]);
12

13
// iterate over plants, planting those with longest grow time first
14
// track the sum of plantTimes
15
// Max of current plantTime sum + growTime[i] for all plants will be earliest bloom day
16
let plantTimeSum = 0,
17
fullBloomDay = -1;
18
for (const plant of sortedByGrowTime) {
19
plantTimeSum += plantTime[plant];
20
fullBloomDay = Math.max(fullBloomDay, plantTimeSum + growTime[plant]);
21
}
22
return fullBloomDay;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0