Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1691
Browse files Browse the repository at this point in the history
No.1691.Maximum Height by Stacking Cuboids
  • Loading branch information
yanglbme committed Apr 9, 2022
1 parent c079fa8 commit 6bed2dd
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 2 deletions.
122 changes: 121 additions & 1 deletion solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,142 @@

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

考虑两个长方体,假设其三边分别是 `(a1, b1, c2)``(a2, b2, c2)`。这里不妨设 `a1≤b1≤c1`, `a2≤b2≤c2`。我们可以发现,假设两个长方体能够拼接到一起(假设第一个长方体较小),则必然有:`a1≤a2, b1≤b2, c1≤c2`

直观上我们可以发现,如果两个长方体能够拼接到一起,则他们可以从任何一个面进行拼接。本题允许我们任意旋转长方体,看起来情况比较复杂,需要讨论 6 种排列情况,但实际上,因为我们希望高度尽可能高,所以根据上面的观察,我们应该选择**从较短的两条边组成的面**进行拼接。

因此,我们进行两次排序:

1. 将每个长方体的三条边按升序排列;
1. 将每个长方体升序排列。

之后,问题转换为最长上升子序列问题。对于第 i 个长方体,我们依次考虑第 `[1...i-1]` 个长方体,看能否将第 i 个长方体拼接在它的下方,然后更新当前的最大值。

时间复杂度 O(n²)。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def maxHeight(self, cuboids: List[List[int]]) -> int:
for c in cuboids:
c.sort()
cuboids.sort()
n = len(cuboids)
dp = [0] * n
for i in range(n):
for j in range(i):
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
dp[i] = max(dp[i], dp[j])
dp[i] += cuboids[i][2]
return max(dp)
```

### **Java**

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

```java
class Solution {
public int maxHeight(int[][] cuboids) {
for (int[] c : cuboids) {
Arrays.sort(c);
}
Arrays.sort(cuboids, (a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
}
if (a[1] != b[1]) {
return a[1] - b[1];
}
return a[2] - b[2];
});
int n = cuboids.length;
int[] dp = new int[n];
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
dp[i] = Math.max(dp[i], dp[j]);
}
}
dp[i] += cuboids[i][2];
ans = Math.max(ans, dp[i]);
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int maxHeight(vector<vector<int>>& cuboids) {
for (auto& c : cuboids) sort(c.begin(), c.end());
sort(cuboids.begin(), cuboids.end());
int n = cuboids.size();
vector<int> dp(n);
int ans = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2])
{
dp[i] = max(dp[i], dp[j]);
}
}
dp[i] += cuboids[i][2];
ans = max(ans, dp[i]);
}
return ans;
}
};
```
### **Go**
```go
func maxHeight(cuboids [][]int) int {
for _, c := range cuboids {
sort.Ints(c)
}
sort.Slice(cuboids, func(i, j int) bool {
if cuboids[i][0] != cuboids[j][0] {
return cuboids[i][0] < cuboids[j][0]
}
if cuboids[i][1] != cuboids[j][1] {
return cuboids[i][1] < cuboids[j][1]
}
return cuboids[i][2] < cuboids[j][2]
})
n := len(cuboids)
dp := make([]int, n)
ans := 0
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j])
}
}
dp[i] += cuboids[i][2]
ans = max(ans, dp[i])
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,120 @@ The maximum height of stacked cuboids is 6 * 17 = 102.
### **Python3**

```python

class Solution:
def maxHeight(self, cuboids: List[List[int]]) -> int:
for c in cuboids:
c.sort()
cuboids.sort()
n = len(cuboids)
dp = [0] * n
for i in range(n):
for j in range(i):
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
dp[i] = max(dp[i], dp[j])
dp[i] += cuboids[i][2]
return max(dp)
```

### **Java**

```java
class Solution {
public int maxHeight(int[][] cuboids) {
for (int[] c : cuboids) {
Arrays.sort(c);
}
Arrays.sort(cuboids, (a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
}
if (a[1] != b[1]) {
return a[1] - b[1];
}
return a[2] - b[2];
});
int n = cuboids.length;
int[] dp = new int[n];
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
dp[i] = Math.max(dp[i], dp[j]);
}
}
dp[i] += cuboids[i][2];
ans = Math.max(ans, dp[i]);
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int maxHeight(vector<vector<int>>& cuboids) {
for (auto& c : cuboids) sort(c.begin(), c.end());
sort(cuboids.begin(), cuboids.end());
int n = cuboids.size();
vector<int> dp(n);
int ans = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2])
{
dp[i] = max(dp[i], dp[j]);
}
}
dp[i] += cuboids[i][2];
ans = max(ans, dp[i]);
}
return ans;
}
};
```
### **Go**
```go
func maxHeight(cuboids [][]int) int {
for _, c := range cuboids {
sort.Ints(c)
}
sort.Slice(cuboids, func(i, j int) bool {
if cuboids[i][0] != cuboids[j][0] {
return cuboids[i][0] < cuboids[j][0]
}
if cuboids[i][1] != cuboids[j][1] {
return cuboids[i][1] < cuboids[j][1]
}
return cuboids[i][2] < cuboids[j][2]
})
n := len(cuboids)
dp := make([]int, n)
ans := 0
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j])
}
}
dp[i] += cuboids[i][2]
ans = max(ans, dp[i])
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int maxHeight(vector<vector<int>>& cuboids) {
for (auto& c : cuboids) sort(c.begin(), c.end());
sort(cuboids.begin(), cuboids.end());
int n = cuboids.size();
vector<int> dp(n);
int ans = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2])
{
dp[i] = max(dp[i], dp[j]);
}
}
dp[i] += cuboids[i][2];
ans = max(ans, dp[i]);
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
func maxHeight(cuboids [][]int) int {
for _, c := range cuboids {
sort.Ints(c)
}
sort.Slice(cuboids, func(i, j int) bool {
if cuboids[i][0] != cuboids[j][0] {
return cuboids[i][0] < cuboids[j][0]
}
if cuboids[i][1] != cuboids[j][1] {
return cuboids[i][1] < cuboids[j][1]
}
return cuboids[i][2] < cuboids[j][2]
})
n := len(cuboids)
dp := make([]int, n)
ans := 0
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j])
}
}
dp[i] += cuboids[i][2]
ans = max(ans, dp[i])
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public int maxHeight(int[][] cuboids) {
for (int[] c : cuboids) {
Arrays.sort(c);
}
Arrays.sort(cuboids, (a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
}
if (a[1] != b[1]) {
return a[1] - b[1];
}
return a[2] - b[2];
});
int n = cuboids.length;
int[] dp = new int[n];
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
dp[i] = Math.max(dp[i], dp[j]);
}
}
dp[i] += cuboids[i][2];
ans = Math.max(ans, dp[i]);
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def maxHeight(self, cuboids: List[List[int]]) -> int:
for c in cuboids:
c.sort()
cuboids.sort()
n = len(cuboids)
dp = [0] * n
for i in range(n):
for j in range(i):
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
dp[i] = max(dp[i], dp[j])
dp[i] += cuboids[i][2]
return max(dp)

0 comments on commit 6bed2dd

Please sign in to comment.