Skip to content

Commit 6bed2dd

Browse files
committed
feat: add solutions to lc problem: No.1691
No.1691.Maximum Height by Stacking Cuboids
1 parent c079fa8 commit 6bed2dd

File tree

6 files changed

+328
-2
lines changed

6 files changed

+328
-2
lines changed

solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README.md

+121-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,142 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
考虑两个长方体,假设其三边分别是 `(a1, b1, c2)``(a2, b2, c2)`。这里不妨设 `a1≤b1≤c1`, `a2≤b2≤c2`。我们可以发现,假设两个长方体能够拼接到一起(假设第一个长方体较小),则必然有:`a1≤a2, b1≤b2, c1≤c2`
67+
68+
直观上我们可以发现,如果两个长方体能够拼接到一起,则他们可以从任何一个面进行拼接。本题允许我们任意旋转长方体,看起来情况比较复杂,需要讨论 6 种排列情况,但实际上,因为我们希望高度尽可能高,所以根据上面的观察,我们应该选择**从较短的两条边组成的面**进行拼接。
69+
70+
因此,我们进行两次排序:
71+
72+
1. 将每个长方体的三条边按升序排列;
73+
1. 将每个长方体升序排列。
74+
75+
之后,问题转换为最长上升子序列问题。对于第 i 个长方体,我们依次考虑第 `[1...i-1]` 个长方体,看能否将第 i 个长方体拼接在它的下方,然后更新当前的最大值。
76+
77+
时间复杂度 O(n²)。
78+
6679
<!-- tabs:start -->
6780

6881
### **Python3**
6982

7083
<!-- 这里可写当前语言的特殊实现逻辑 -->
7184

7285
```python
73-
86+
class Solution:
87+
def maxHeight(self, cuboids: List[List[int]]) -> int:
88+
for c in cuboids:
89+
c.sort()
90+
cuboids.sort()
91+
n = len(cuboids)
92+
dp = [0] * n
93+
for i in range(n):
94+
for j in range(i):
95+
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
96+
dp[i] = max(dp[i], dp[j])
97+
dp[i] += cuboids[i][2]
98+
return max(dp)
7499
```
75100

76101
### **Java**
77102

78103
<!-- 这里可写当前语言的特殊实现逻辑 -->
79104

80105
```java
106+
class Solution {
107+
public int maxHeight(int[][] cuboids) {
108+
for (int[] c : cuboids) {
109+
Arrays.sort(c);
110+
}
111+
Arrays.sort(cuboids, (a, b) -> {
112+
if (a[0] != b[0]) {
113+
return a[0] - b[0];
114+
}
115+
if (a[1] != b[1]) {
116+
return a[1] - b[1];
117+
}
118+
return a[2] - b[2];
119+
});
120+
int n = cuboids.length;
121+
int[] dp = new int[n];
122+
int ans = 0;
123+
for (int i = 0; i < n; ++i) {
124+
for (int j = 0; j < i; ++j) {
125+
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
126+
dp[i] = Math.max(dp[i], dp[j]);
127+
}
128+
}
129+
dp[i] += cuboids[i][2];
130+
ans = Math.max(ans, dp[i]);
131+
}
132+
return ans;
133+
}
134+
}
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
int maxHeight(vector<vector<int>>& cuboids) {
143+
for (auto& c : cuboids) sort(c.begin(), c.end());
144+
sort(cuboids.begin(), cuboids.end());
145+
int n = cuboids.size();
146+
vector<int> dp(n);
147+
int ans = 0;
148+
for (int i = 0; i < n; ++i)
149+
{
150+
for (int j = 0; j < i; ++j)
151+
{
152+
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2])
153+
{
154+
dp[i] = max(dp[i], dp[j]);
155+
}
156+
}
157+
dp[i] += cuboids[i][2];
158+
ans = max(ans, dp[i]);
159+
}
160+
return ans;
161+
}
162+
};
163+
```
81164
165+
### **Go**
166+
167+
```go
168+
func maxHeight(cuboids [][]int) int {
169+
for _, c := range cuboids {
170+
sort.Ints(c)
171+
}
172+
sort.Slice(cuboids, func(i, j int) bool {
173+
if cuboids[i][0] != cuboids[j][0] {
174+
return cuboids[i][0] < cuboids[j][0]
175+
}
176+
if cuboids[i][1] != cuboids[j][1] {
177+
return cuboids[i][1] < cuboids[j][1]
178+
}
179+
return cuboids[i][2] < cuboids[j][2]
180+
})
181+
n := len(cuboids)
182+
dp := make([]int, n)
183+
ans := 0
184+
for i := 0; i < n; i++ {
185+
for j := 0; j < i; j++ {
186+
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
187+
dp[i] = max(dp[i], dp[j])
188+
}
189+
}
190+
dp[i] += cuboids[i][2]
191+
ans = max(ans, dp[i])
192+
}
193+
return ans
194+
}
195+
196+
func max(a, b int) int {
197+
if a > b {
198+
return a
199+
}
200+
return b
201+
}
82202
```
83203

84204
### **...**

solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README_EN.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,120 @@ The maximum height of stacked cuboids is 6 * 17 = 102.
6262
### **Python3**
6363

6464
```python
65-
65+
class Solution:
66+
def maxHeight(self, cuboids: List[List[int]]) -> int:
67+
for c in cuboids:
68+
c.sort()
69+
cuboids.sort()
70+
n = len(cuboids)
71+
dp = [0] * n
72+
for i in range(n):
73+
for j in range(i):
74+
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
75+
dp[i] = max(dp[i], dp[j])
76+
dp[i] += cuboids[i][2]
77+
return max(dp)
6678
```
6779

6880
### **Java**
6981

7082
```java
83+
class Solution {
84+
public int maxHeight(int[][] cuboids) {
85+
for (int[] c : cuboids) {
86+
Arrays.sort(c);
87+
}
88+
Arrays.sort(cuboids, (a, b) -> {
89+
if (a[0] != b[0]) {
90+
return a[0] - b[0];
91+
}
92+
if (a[1] != b[1]) {
93+
return a[1] - b[1];
94+
}
95+
return a[2] - b[2];
96+
});
97+
int n = cuboids.length;
98+
int[] dp = new int[n];
99+
int ans = 0;
100+
for (int i = 0; i < n; ++i) {
101+
for (int j = 0; j < i; ++j) {
102+
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
103+
dp[i] = Math.max(dp[i], dp[j]);
104+
}
105+
}
106+
dp[i] += cuboids[i][2];
107+
ans = Math.max(ans, dp[i]);
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int maxHeight(vector<vector<int>>& cuboids) {
120+
for (auto& c : cuboids) sort(c.begin(), c.end());
121+
sort(cuboids.begin(), cuboids.end());
122+
int n = cuboids.size();
123+
vector<int> dp(n);
124+
int ans = 0;
125+
for (int i = 0; i < n; ++i)
126+
{
127+
for (int j = 0; j < i; ++j)
128+
{
129+
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2])
130+
{
131+
dp[i] = max(dp[i], dp[j]);
132+
}
133+
}
134+
dp[i] += cuboids[i][2];
135+
ans = max(ans, dp[i]);
136+
}
137+
return ans;
138+
}
139+
};
140+
```
71141
142+
### **Go**
143+
144+
```go
145+
func maxHeight(cuboids [][]int) int {
146+
for _, c := range cuboids {
147+
sort.Ints(c)
148+
}
149+
sort.Slice(cuboids, func(i, j int) bool {
150+
if cuboids[i][0] != cuboids[j][0] {
151+
return cuboids[i][0] < cuboids[j][0]
152+
}
153+
if cuboids[i][1] != cuboids[j][1] {
154+
return cuboids[i][1] < cuboids[j][1]
155+
}
156+
return cuboids[i][2] < cuboids[j][2]
157+
})
158+
n := len(cuboids)
159+
dp := make([]int, n)
160+
ans := 0
161+
for i := 0; i < n; i++ {
162+
for j := 0; j < i; j++ {
163+
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
164+
dp[i] = max(dp[i], dp[j])
165+
}
166+
}
167+
dp[i] += cuboids[i][2]
168+
ans = max(ans, dp[i])
169+
}
170+
return ans
171+
}
172+
173+
func max(a, b int) int {
174+
if a > b {
175+
return a
176+
}
177+
return b
178+
}
72179
```
73180

74181
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int maxHeight(vector<vector<int>>& cuboids) {
4+
for (auto& c : cuboids) sort(c.begin(), c.end());
5+
sort(cuboids.begin(), cuboids.end());
6+
int n = cuboids.size();
7+
vector<int> dp(n);
8+
int ans = 0;
9+
for (int i = 0; i < n; ++i)
10+
{
11+
for (int j = 0; j < i; ++j)
12+
{
13+
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2])
14+
{
15+
dp[i] = max(dp[i], dp[j]);
16+
}
17+
}
18+
dp[i] += cuboids[i][2];
19+
ans = max(ans, dp[i]);
20+
}
21+
return ans;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func maxHeight(cuboids [][]int) int {
2+
for _, c := range cuboids {
3+
sort.Ints(c)
4+
}
5+
sort.Slice(cuboids, func(i, j int) bool {
6+
if cuboids[i][0] != cuboids[j][0] {
7+
return cuboids[i][0] < cuboids[j][0]
8+
}
9+
if cuboids[i][1] != cuboids[j][1] {
10+
return cuboids[i][1] < cuboids[j][1]
11+
}
12+
return cuboids[i][2] < cuboids[j][2]
13+
})
14+
n := len(cuboids)
15+
dp := make([]int, n)
16+
ans := 0
17+
for i := 0; i < n; i++ {
18+
for j := 0; j < i; j++ {
19+
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
20+
dp[i] = max(dp[i], dp[j])
21+
}
22+
}
23+
dp[i] += cuboids[i][2]
24+
ans = max(ans, dp[i])
25+
}
26+
return ans
27+
}
28+
29+
func max(a, b int) int {
30+
if a > b {
31+
return a
32+
}
33+
return b
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int maxHeight(int[][] cuboids) {
3+
for (int[] c : cuboids) {
4+
Arrays.sort(c);
5+
}
6+
Arrays.sort(cuboids, (a, b) -> {
7+
if (a[0] != b[0]) {
8+
return a[0] - b[0];
9+
}
10+
if (a[1] != b[1]) {
11+
return a[1] - b[1];
12+
}
13+
return a[2] - b[2];
14+
});
15+
int n = cuboids.length;
16+
int[] dp = new int[n];
17+
int ans = 0;
18+
for (int i = 0; i < n; ++i) {
19+
for (int j = 0; j < i; ++j) {
20+
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
21+
dp[i] = Math.max(dp[i], dp[j]);
22+
}
23+
}
24+
dp[i] += cuboids[i][2];
25+
ans = Math.max(ans, dp[i]);
26+
}
27+
return ans;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxHeight(self, cuboids: List[List[int]]) -> int:
3+
for c in cuboids:
4+
c.sort()
5+
cuboids.sort()
6+
n = len(cuboids)
7+
dp = [0] * n
8+
for i in range(n):
9+
for j in range(i):
10+
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
11+
dp[i] = max(dp[i], dp[j])
12+
dp[i] += cuboids[i][2]
13+
return max(dp)

0 commit comments

Comments
 (0)