website/content.en/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md
Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset of cuboids and place them on each other.
You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid.
Return the maximum height of the stacked cuboids.
Example 1:
Input: cuboids = [[50,45,20],[95,37,53],[45,23,12]]
Output: 190
Explanation:
Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95.
Cuboid 0 is placed next with the 45x20 side facing down with height 50.
Cuboid 2 is placed next with the 23x12 side facing down with height 45.
The total height is 95 + 50 + 45 = 190.
Example 2:
Input: cuboids = [[38,25,45],[76,35,3]]
Output: 76
Explanation:
You can't place any of the cuboids on the other.
We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
Example 3:
Input: cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
Output: 102
Explanation:
After rearranging the cuboids, you can see that all cuboids have the same dimension.
You can place the 11x7 side down on all cuboids so their heights are 17.
The maximum height of stacked cuboids is 6 * 17 = 102.
Constraints:
n == cuboids.length1 <= n <= 1001 <= widthi, lengthi, heighti <= 100You are given n cuboids, where the length, width, and height of the ith cuboid are represented as cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset from cuboids and stack them. If widthi <= widthj, lengthi <= lengthj, and heighti <= heightj, you can stack cuboid i on cuboid j. You can rearrange a cuboid's length, width, and height by rotating it so that it can be placed on another cuboid. Return the maximum height that can be obtained by stacking the cuboids.
dp[i] as the maximum height that can be stacked with i as the last cuboid. Since length and width have already been sorted, we only need to dynamically update the maximum dp value within the interval [0, i - 1].package leetcode
import "sort"
func maxHeight(cuboids [][]int) int {
n := len(cuboids)
for i := 0; i < n; i++ {
sort.Ints(cuboids[i]) // Sort the three sides of each cuboid
}
// Sort cuboids, first by the shortest side, then by the longest side
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]
})
res := 0
dp := make([]int, n)
for i := 0; i < n; i++ {
dp[i] = cuboids[i][2]
res = max(res, dp[i])
}
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][0] <= cuboids[i][0] && cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j]+cuboids[i][2])
}
}
res = max(res, dp[i])
}
return res
}
func max(x, y int) int {
if x > y {
return x
}
return y
}