Skip to content

Commit b3f70fb

Browse files
authored
Create maximum-height-by-stacking-cuboids.cpp
1 parent 6f9f667 commit b3f70fb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(n^2)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int maxHeight(vector<vector<int>>& cuboids) {
7+
for (auto& cuboid : cuboids) {
8+
sort(begin(cuboid), end(cuboid));
9+
}
10+
cuboids.push_back({0, 0, 0});
11+
sort(begin(cuboids), end(cuboids));
12+
vector<int> dp(size(cuboids));
13+
for (int i = 1; i < size(cuboids); ++i) {
14+
for (int j = 0; j < i; ++j) {
15+
if (cuboids[j][0] <= cuboids[i][0] &&
16+
cuboids[j][1] <= cuboids[i][1] &&
17+
cuboids[j][2] <= cuboids[i][2]) {
18+
dp[i] = max(dp[i], dp[j] + cuboids[i][2]);
19+
}
20+
}
21+
}
22+
return *max_element(cbegin(dp), cend(dp));
23+
}
24+
};

0 commit comments

Comments
 (0)