Earliest Possible Day of Full Bloom LeetCode solution in Java

1
class Solution {
2
public int earliestFullBloom(int[] plantTime, int[] growTime) {
3
int n = plantTime.length;
4
int[][] plants = new int[n][2];
5
int totalPlantTime = 0;
6
for (int i = 0; i < n; i++) {
7
plants[i] = new int[] {plantTime[i], growTime[i]};
8
totalPlantTime += plantTime[i];
9
}
10
// Sort plants by grow time.
11
// The plant with fewer grow time can be planted at the end.
12
Arrays.sort(plants, (a, b) -> a[1] - b[1]);
13
// Max grow + plant time
14
int maxTime = 0;
15
for (int[] plant : plants) {
16
maxTime = Math.max(maxTime, totalPlantTime + plant[1]);
17
// After putting this plant at the end of the chain,
18
// we can take the current plant time out of the total plant time
19
totalPlantTime -= plant[0];
20
}
21
return maxTime;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0