forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1691
No.1691.Maximum Height by Stacking Cuboids
- Loading branch information
Showing
6 changed files
with
328 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
34 changes: 34 additions & 0 deletions
34
solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
29 changes: 29 additions & 0 deletions
29
solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |