Skip to content

Commit fb91d65

Browse files
committedApr 26, 2025
Add solution #1691
1 parent a5fd1db commit fb91d65

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,478 LeetCode solutions in JavaScript
1+
# 1,479 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1303,6 +1303,7 @@
13031303
1688|[Count of Matches in Tournament](./solutions/1688-count-of-matches-in-tournament.js)|Easy|
13041304
1689|[Partitioning Into Minimum Number Of Deci-Binary Numbers](./solutions/1689-partitioning-into-minimum-number-of-deci-binary-numbers.js)|Medium|
13051305
1690|[Stone Game VII](./solutions/1690-stone-game-vii.js)|Medium|
1306+
1691|[Maximum Height by Stacking Cuboids](./solutions/1691-maximum-height-by-stacking-cuboids.js)|Hard|
13061307
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13071308
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13081309
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1691. Maximum Height by Stacking Cuboids
3+
* https://leetcode.com/problems/maximum-height-by-stacking-cuboids/
4+
* Difficulty: Hard
5+
*
6+
* Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti]
7+
* (0-indexed). Choose a subset of cuboids and place them on each other.
8+
*
9+
* You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and
10+
* heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on
11+
* another cuboid.
12+
*
13+
* Return the maximum height of the stacked cuboids.
14+
*/
15+
16+
/**
17+
* @param {number[][]} cuboids
18+
* @return {number}
19+
*/
20+
var maxHeight = function(cuboids) {
21+
const sortedCuboids = cuboids.map(dim => dim.sort((a, b) => a - b))
22+
.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
23+
const n = sortedCuboids.length;
24+
const maxHeights = new Array(n).fill(0);
25+
let result = 0;
26+
27+
for (let i = 0; i < n; i++) {
28+
maxHeights[i] = sortedCuboids[i][2];
29+
for (let j = 0; j < i; j++) {
30+
if (sortedCuboids[j][0] <= sortedCuboids[i][0]
31+
&& sortedCuboids[j][1] <= sortedCuboids[i][1]
32+
&& sortedCuboids[j][2] <= sortedCuboids[i][2]) {
33+
maxHeights[i] = Math.max(maxHeights[i], maxHeights[j] + sortedCuboids[i][2]);
34+
}
35+
}
36+
result = Math.max(result, maxHeights[i]);
37+
}
38+
39+
return result;
40+
};

0 commit comments

Comments
 (0)
Please sign in to comment.