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.length
1 <= n <= 100
1 <= widthi, lengthi, heighti <= 100
Related Topics:
Dynamic Programming, Sort
We firstly sort the edges of each cuboid in descending order so that we can use its first edge as its height. (See proof below)
We say cuboid x
is greaterThan
than cuboid y
if x[0] >= y[0] && x[1] >= y[1] && x[2] >= y[2]
.
Let dp[i]
be the maximum height of stack we can get with cuboids[i]
at the bottom.
dp[i] = cuboids[i][0] + max( dp[j] | 0 <= j < N && i != j && A[i] is greater than A[j] )
The answer is the maximum value of the dp
values.
Sorting the edges of all cuboids takes O(N)
time.
For the DP part, each 0 <= i < N
is only visited once. Each visit takes O(N)
time. So the time complexity of this top-down DP is O(N^2)
.
So overall the time complexity is O(N^2)
.
// OJ: https://leetcode.com/problems/maximum-height-by-stacking-cuboids/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N)
class Solution {
vector<int> dp;
int N;
bool greaterThan(vector<vector<int>> &A, int i, int j) {
return A[i][0] >= A[j][0] && A[i][1] >= A[j][1] && A[i][2] >= A[j][2];
}
int dfs(vector<vector<int>> &A, int i) {
if (dp[i] != -1) return dp[i];
dp[i] = 0;
int ans = 0;
for (int j = 0; j < N; ++j) {
if (i == j) continue;
if (greaterThan(A, i, j)) ans = max(ans, dfs(A, j));
}
return dp[i] = A[i][0] + ans;
}
public:
int maxHeight(vector<vector<int>>& A) {
for (auto &c : A) sort(begin(c), end(c), greater<>());
N = A.size();
dp.assign(N, -1);
for (int i = 0; i < N; ++i) dfs(A, i);
return *max_element(begin(dp), end(dp));
}
};
Is it possible that in the optimal solution, one cuboid is not placed with its longest edge as height? No, we can prove that this is impossible
Proof: assume in the optimal solution one cuboid x
is not placed with its longest edge as height. For the cuboid y
below x
, all edges of y
must be greater than x
. So we can flip both x
and y
and all cuboids below such that their longest edges are placed as their height. The same for the cuboids above x
.