Maximum Height by Stacking Cuboids LeetCode solution in C++

1
class Solution {
2
public:
3
int maxHeight(vector<vector<int>> &cuboids) {
4
ios_base::sync_with_stdio(false);
5
cin.tie(NULL);
6
for (auto &x : cuboids) {
7
sort(x.begin(), x.end());
8
}
9

10
sort(cuboids.begin(), cuboids.end());
11

12
int n = cuboids.size();
13
vector<int> dp(n, 0);
14
for (int i = 0; i < n; i++) {
15
dp[i] = cuboids[i][2];
16
for (int j = 0; j < i; j++) {
17
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
18
dp[i] = max(dp[i], dp[j] + cuboids[i][2]);
19
}
20
}
21
}
22
return *max_element(dp.begin(), dp.end());
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0