Maximum Height by Stacking Cuboids LeetCode solution in C++
1class Solution {2public:3int maxHeight(vector<vector<int>> &cuboids) {4ios_base::sync_with_stdio(false);5cin.tie(NULL);6for (auto &x : cuboids) {7sort(x.begin(), x.end());8}910sort(cuboids.begin(), cuboids.end());1112int n = cuboids.size();13vector<int> dp(n, 0);14for (int i = 0; i < n; i++) {15dp[i] = cuboids[i][2];16for (int j = 0; j < i; j++) {17if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {18dp[i] = max(dp[i], dp[j] + cuboids[i][2]);19}20}21}22return *max_element(dp.begin(), dp.end());23}24};