-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathMaximumHeightByStackingCuboids.cpp
More file actions
43 lines (35 loc) · 1.19 KB
/
MaximumHeightByStackingCuboids.cpp
File metadata and controls
43 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// https://leetcode.com/problems/maximum-height-by-stacking-cuboids/description/
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(vector<vector<int>>& cuboids) {
int n= cuboids.size();
if(n==0)
return 0;
vector<int> current(n+1, 0);
vector<int> next(n+1, 0);
for(int curr= n-1; curr>=0; curr--){
for(int prev= curr-1; prev>=-1; prev--){
int include= 0;
if(prev==-1 || check(cuboids[curr], cuboids[prev]))
include= cuboids[curr][2] + next[curr+1];
int exclude= 0 + next[prev+1];
current[prev+1]= max(include, exclude);
}
next= current;
}
return next[0];
}
int maxHeight(vector<vector<int>>& cuboids) {
for(auto &dimensions : cuboids){
sort(dimensions.begin(), dimensions.end());
}
sort(cuboids.begin(), cuboids.end());
return solve(cuboids);
}
};