Skip to content

Commit 1dae647

Browse files
committed
feat: leetcode 1691
1 parent 5cd0f82 commit 1dae647

File tree

8 files changed

+143
-1
lines changed

8 files changed

+143
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:567-green" alt="进度:567">
5+
<img src="https://img.shields.io/badge/-进度:568-green" alt="进度:568">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,12 @@
18011801
"path": "./problemset/delivering-boxes-from-storage-to-ports/README.md",
18021802
"difficulty": "困难"
18031803
},
1804+
{
1805+
"id": "1691",
1806+
"title": "堆叠长方体的最大高度",
1807+
"path": "./problemset/maximum-height-by-stacking-cuboids/README.md",
1808+
"difficulty": "困难"
1809+
},
18041810
{
18051811
"id": "1774",
18061812
"title": "最接近目标价格的甜点成本",

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -5079,6 +5079,16 @@
50795079
"url": "https://leetcode.cn/problems/delivering-boxes-from-storage-to-ports/",
50805080
"path": "./problemset/delivering-boxes-from-storage-to-ports/README.md"
50815081
},
5082+
{
5083+
"id": "1691",
5084+
"title": {
5085+
"cn": "堆叠长方体的最大高度",
5086+
"en": "maximum-height-by-stacking-cuboids"
5087+
},
5088+
"difficulty": "困难",
5089+
"url": "https://leetcode.cn/problems/maximum-height-by-stacking-cuboids/",
5090+
"path": "./problemset/maximum-height-by-stacking-cuboids/README.md"
5091+
},
50825092
{
50835093
"id": "1694",
50845094
"title": {

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
| 1235. [规划兼职工作](../../problemset/maximum-profit-in-job-scheduling/README.md) | 困难 |
334334
| 1668. [最大重复子字符串](../../problemset/maximum-repeating-substring/README.md) | 简单 |
335335
| 1687. [从仓库到码头运输箱子](../../problemset/delivering-boxes-from-storage-to-ports/README.md) | 困难 |
336+
| 1691. [堆叠长方体的最大高度](../../problemset/maximum-height-by-stacking-cuboids/README.md) | 困难 |
336337
| 1774. [最接近目标价格的甜点成本](../../problemset/closest-dessert-cost/README.md) | 中等 |
337338
| 1800. [最大升序子数组和](../../problemset/maximum-ascending-subarray-sum/README.md) | 简单 |
338339
| 1994. [好子集的数目](../../problemset/the-number-of-good-subsets/README.md) | 困难 |

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,8 @@
10161016

10171017
[1687. 从仓库到码头运输箱子](../../problemset/delivering-boxes-from-storage-to-ports/README.md)
10181018

1019+
[1691. 堆叠长方体的最大高度](../../problemset/maximum-height-by-stacking-cuboids/README.md)
1020+
10191021
[1694. 重新格式化电话号码](../../problemset/reformat-phone-number/README.md)
10201022

10211023
[1700. 无法吃午餐的学生数量](../../problemset/number-of-students-unable-to-eat-lunch/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 堆叠长方体的最大高度
2+
3+
> 难度:困难
4+
>
5+
> https://leetcode.cn/problems/maximum-height-by-stacking-cuboids/
6+
7+
## 题目
8+
9+
给你 `n` 个长方体 `cuboids` ,其中第 `i` 个长方体的长宽高表示为 `cuboids[i] = [widthi, lengthi, heighti]`(下标从 0 开始)。请你从 `cuboids` 选出一个 **子集** ,并将它们堆叠起来。
10+
11+
如果 `widthi <= widthj``lengthi <= lengthj``heighti <= heightj` ,你就可以将长方体 `i` 堆叠在长方体 `j` 上。你可以通过旋转把长方体的长宽高重新排列,以将它放在另一个长方体上。
12+
13+
**返回** 堆叠长方体 `cuboids` 可以得到的 **最大高度**
14+
15+
### 示例
16+
17+
#### 示例 1:
18+
19+
![image](https://user-images.githubusercontent.com/54696834/206861783-2d077827-3670-4feb-9434-24306700d5a0.png)
20+
21+
```
22+
输入:cuboids = [[50,45,20],[95,37,53],[45,23,12]]
23+
输出:190
24+
解释:
25+
第 1 个长方体放在底部,53x37 的一面朝下,高度为 95 。
26+
第 0 个长方体放在中间,45x20 的一面朝下,高度为 50 。
27+
第 2 个长方体放在上面,23x12 的一面朝下,高度为 45 。
28+
总高度是 95 + 50 + 45 = 190 。
29+
```
30+
31+
#### 示例 2:
32+
33+
```
34+
输入:cuboids = [[38,25,45],[76,35,3]]
35+
输出:76
36+
解释:
37+
无法将任何长方体放在另一个上面。
38+
选择第 1 个长方体然后旋转它,使 35x3 的一面朝下,其高度为 76 。
39+
```
40+
41+
#### 示例 3:
42+
43+
```
44+
输入:cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
45+
输出:102
46+
解释:
47+
重新排列长方体后,可以看到所有长方体的尺寸都相同。
48+
你可以把 11x7 的一面朝下,这样它们的高度就是 17 。
49+
堆叠长方体的最大高度为 6 * 17 = 102 。
50+
```
51+
52+
## 解题
53+
54+
```ts
55+
/**
56+
* 动态规划
57+
* @desc 时间复杂度 O(n^2) 空间复杂度 O(n)
58+
* @param cuboids
59+
* @returns
60+
*/
61+
export function maxHeight(cuboids: number[][]): number {
62+
const n = cuboids.length
63+
for (const v of cuboids)
64+
v.sort((a, b) => a - b)
65+
66+
cuboids.sort((a, b) => (a[0] + a[1] + a[2]) - (b[0] + b[1] + b[2]))
67+
let ans = 0
68+
const dp = new Array(n).fill(0)
69+
for (let i = 0; i < n; i++) {
70+
dp[i] = cuboids[i][2]
71+
for (let j = 0; j < i; j++) {
72+
if (cuboids[i][0] >= cuboids[j][0]
73+
&& cuboids[i][1] >= cuboids[j][1]
74+
&& cuboids[i][2] >= cuboids[j][2])
75+
dp[i] = Math.max(dp[i], dp[j] + cuboids[i][2])
76+
}
77+
ans = Math.max(ans, dp[i])
78+
}
79+
return ans
80+
}
81+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { maxHeight } from '.'
3+
4+
describe('堆叠长方体的最大高度', () => {
5+
testCase(maxHeight)
6+
})
7+
8+
function testCase(fn: (cuboids: number[][]) => number) {
9+
it.each([
10+
[[[50, 45, 20], [95, 37, 53], [45, 23, 12]], 190],
11+
[[[38, 25, 45], [76, 35, 3]], 76],
12+
[[[7, 11, 17], [7, 17, 11], [11, 7, 17], [11, 17, 7], [17, 7, 11], [17, 11, 7]], 102],
13+
])('示例%#', (cuboids, expected) => {
14+
expect(fn(cuboids)).toBe(expected)
15+
})
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 动态规划
3+
* @desc 时间复杂度 O(n^2) 空间复杂度 O(n)
4+
* @param cuboids
5+
* @returns
6+
*/
7+
export function maxHeight(cuboids: number[][]): number {
8+
const n = cuboids.length
9+
for (const v of cuboids)
10+
v.sort((a, b) => a - b)
11+
12+
cuboids.sort((a, b) => (a[0] + a[1] + a[2]) - (b[0] + b[1] + b[2]))
13+
let ans = 0
14+
const dp = new Array(n).fill(0)
15+
for (let i = 0; i < n; i++) {
16+
dp[i] = cuboids[i][2]
17+
for (let j = 0; j < i; j++) {
18+
if (cuboids[i][0] >= cuboids[j][0]
19+
&& cuboids[i][1] >= cuboids[j][1]
20+
&& cuboids[i][2] >= cuboids[j][2])
21+
dp[i] = Math.max(dp[i], dp[j] + cuboids[i][2])
22+
}
23+
ans = Math.max(ans, dp[i])
24+
}
25+
return ans
26+
}

0 commit comments

Comments
 (0)