<p>Given <code>n</code> <code>cuboids</code> where the dimensions of the <code>i<sup>th</sup></code> cuboid is <code>cuboids[i] = [width<sub>i</sub>, length<sub>i</sub>, height<sub>i</sub>]</code> (<strong>0-indexed</strong>). Choose a <strong>subset</strong> of <code>cuboids</code> and place them on each other.</p>

<p>You can place cuboid <code>i</code> on cuboid <code>j</code> if <code>width<sub>i</sub> &lt;= width<sub>j</sub></code> and <code>length<sub>i</sub> &lt;= length<sub>j</sub></code> and <code>height<sub>i</sub> &lt;= height<sub>j</sub></code>. You can rearrange any cuboid&#39;s dimensions by rotating it to put it on another cuboid.</p>

<p>Return <em>the <strong>maximum height</strong> of the stacked</em> <code>cuboids</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/21/image.jpg" style="width: 420px; height: 299px;" /></strong></p>

<pre>
<strong>Input:</strong> cuboids = [[50,45,20],[95,37,53],[45,23,12]]
<strong>Output:</strong> 190
<strong>Explanation:</strong>
Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95.
Cuboid 0 is placed next with the 45x20 side facing down with height 50.
Cuboid 2 is placed next with the 23x12 side facing down with height 45.
The total height is 95 + 50 + 45 = 190.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cuboids = [[38,25,45],[76,35,3]]
<strong>Output:</strong> 76
<strong>Explanation:</strong>
You can&#39;t place any of the cuboids on the other.
We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
<strong>Output:</strong> 102
<strong>Explanation:</strong>
After rearranging the cuboids, you can see that all cuboids have the same dimension.
You can place the 11x7 side down on all cuboids so their heights are 17.
The maximum height of stacked cuboids is 6 * 17 = 102.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == cuboids.length</code></li>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= width<sub>i</sub>, length<sub>i</sub>, height<sub>i</sub> &lt;= 100</code></li>
</ul>

class Solution {
public:
// kya  mai current cuboid ko previous cuboid per rakh sakta hoon ya nahi
    bool check(vector<int>& curr,vector<int>& prev){
        // curr bade wale dibbe ko aur prev chote wale dibbe ko show kar aha hai
        if(prev[0]<=curr[0] && prev[1]<=curr[1] && prev[2]<=curr[2]){
            return true;
        }
        else{
            return false;
        }
    }
    int solveUsingTabulationSO(vector<vector<int>>& cuboids){
        // create 2d dp array
        int n=cuboids.size();
        // yaha per zero se initialize karne ka fayda yeh hai ki base case ko bhi handle kar le rahe hai
    // yaha per 2d array ki need nahi hai
        // vector<vector<int> >dp(n+1,vector<int>(n+1,0));
    vector<int>currRow(n+1,0);
    vector<int>nextRow(n+1,0);
        // row wise traversal ho raha hai
        for(int curr_index=n-1;curr_index>=0;curr_index--){
           for(int prev_index=curr_index-1;prev_index>=-1;prev_index--){
                int include=0;
                if(prev_index==-1|| check(cuboids[curr_index],cuboids[prev_index])){
                  int heightToAdd=cuboids[curr_index][2];
                  include = heightToAdd  + nextRow[curr_index+1];
                } 
                // prev wale index ki index shifting ki hai
                int exclude = 0 + nextRow[prev_index+1];

                currRow[prev_index+1]=max(include,exclude);
            }
            // shifting
            nextRow=currRow; 
        }
        // recursion me initial value kya send ki thi
        // prev=-1 curr=0
        // but due to invalid index we have to add 1 in prev
        // -1+1=0
        return nextRow[0];
    }
    int maxHeight(vector<vector<int>>& cuboids) {
        // cuboid ko byreference isliye liya hai taki actual cuboid ko sort kare
        // na ki cuboid ki copy ko sort kare  
        for(auto &cuboid:cuboids){
            sort(cuboid.begin(),cuboid.end());
        }
        sort(cuboids.begin(),cuboids.end());
        for(auto i:cuboids){
            for(auto j:i){
                cout<<j<<" ";
            }
            cout<<endl;
        }
        int ans=solveUsingTabulationSO(cuboids);
        return ans;
    }
};
12700