-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path1691-maximum-height-by-stacking-cuboids.js
40 lines (37 loc) · 1.31 KB
/
1691-maximum-height-by-stacking-cuboids.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 1691. Maximum Height by Stacking Cuboids
* https://leetcode.com/problems/maximum-height-by-stacking-cuboids/
* Difficulty: Hard
*
* Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti]
* (0-indexed). Choose a subset of cuboids and place them on each other.
*
* You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and
* heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on
* another cuboid.
*
* Return the maximum height of the stacked cuboids.
*/
/**
* @param {number[][]} cuboids
* @return {number}
*/
var maxHeight = function(cuboids) {
const sortedCuboids = cuboids.map(dim => dim.sort((a, b) => a - b))
.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
const n = sortedCuboids.length;
const maxHeights = new Array(n).fill(0);
let result = 0;
for (let i = 0; i < n; i++) {
maxHeights[i] = sortedCuboids[i][2];
for (let j = 0; j < i; j++) {
if (sortedCuboids[j][0] <= sortedCuboids[i][0]
&& sortedCuboids[j][1] <= sortedCuboids[i][1]
&& sortedCuboids[j][2] <= sortedCuboids[i][2]) {
maxHeights[i] = Math.max(maxHeights[i], maxHeights[j] + sortedCuboids[i][2]);
}
}
result = Math.max(result, maxHeights[i]);
}
return result;
};