Skip to content

Commit b9d0084

Browse files
authored
Update 1691-maximum-height-by-stacking-cuboids.js
1 parent e8a66df commit b9d0084

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

1691-maximum-height-by-stacking-cuboids.js

+31
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,34 @@ var maxHeight = function (cuboids) {
3030
return b[2] - a[2]
3131
}
3232
}
33+
34+
// another
35+
36+
/**
37+
* @param {number[][]} cuboids
38+
* @return {number}
39+
*/
40+
var maxHeight = function(cuboids) {
41+
cuboids.forEach((cuboid) => cuboid.sort((a, b) => a - b));
42+
cuboids.sort((a, b) => {
43+
if (a[0] !== b[0]) return b[0] - a[0];
44+
if (a[1] !== b[1]) return b[1] - a[1];
45+
return b[2] - a[2];
46+
});
47+
const n = cuboids.length;
48+
const dp = Array(n).fill(0);
49+
let res = 0;
50+
for (let j = 0; j < n; ++j) {
51+
dp[j] = cuboids[j][2];
52+
for (let i = 0; i < j; ++i) {
53+
if (cuboids[i][0] >= cuboids[j][0]
54+
&& cuboids[i][1] >= cuboids[j][1]
55+
&& cuboids[i][2] >= cuboids[j][2]
56+
) {
57+
dp[j] = Math.max(dp[j], dp[i] + cuboids[j][2]);
58+
}
59+
}
60+
res = Math.max(res, dp[j]);
61+
}
62+
return res;
63+
};

0 commit comments

Comments
 (0)