|
63 | 63 |
|
64 | 64 | <!-- 这里可写通用的实现逻辑 -->
|
65 | 65 |
|
| 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 | + |
66 | 79 | <!-- tabs:start -->
|
67 | 80 |
|
68 | 81 | ### **Python3**
|
69 | 82 |
|
70 | 83 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 84 |
|
72 | 85 | ```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) |
74 | 99 | ```
|
75 | 100 |
|
76 | 101 | ### **Java**
|
77 | 102 |
|
78 | 103 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
79 | 104 |
|
80 | 105 | ```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 | +``` |
81 | 164 |
|
| 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 | +} |
82 | 202 | ```
|
83 | 203 |
|
84 | 204 | ### **...**
|
|
0 commit comments