Skip to content

Commit

Permalink
Maximum height By Stacking Cuboids
Browse files Browse the repository at this point in the history
  • Loading branch information
Amitesh-tiwari committed Apr 9, 2024
1 parent a436f56 commit 87cd8d7
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions MaximumHeightByStackingCuboids.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.
class Solution {
public:
bool check(vector<int> base, vector<int> newBox){
if(newBox[0] <= base[0] && newBox[1] <= base[1] && newBox[2] <= base[2]){
return true;
}
else{
return false;
}
}
int solve(int n, vector<vector<int>>& a){
vector<int> currRow(n+1,0);
vector<int> nextRow(n+1,0);
for(int curr = n-1; curr >= 0; curr--){
for(int prev = curr-1; prev >= -1; prev--){
int take = 0;
if(prev == -1 || check(a[curr] , a[prev])){
take = a[curr][2] + nextRow[curr + 1];
}
int notTake = 0 + nextRow[prev + 1];
currRow[prev + 1] = max(take, notTake);
}
nextRow = currRow;
}
return nextRow[0];
}
int maxHeight(vector<vector<int>>& cuboids) {
for(auto &a : cuboids){
sort(a.begin(), a.end());
}
sort(cuboids.begin(), cuboids.end());
return solve(cuboids.size(), cuboids);
}
};

0 comments on commit 87cd8d7

Please sign in to comment.