<p>You are given a 2D array of integers <code>envelopes</code> where <code>envelopes[i] = [w<sub>i</sub>, h<sub>i</sub>]</code> represents the width and the height of an envelope.</p>

<p>One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope&#39;s width and height.</p>

<p>Return <em>the maximum number of envelopes you can Russian doll (i.e., put one inside the other)</em>.</p>

<p><strong>Note:</strong> You cannot rotate an envelope.</p>

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

<pre>
<strong>Input:</strong> envelopes = [[5,4],[6,4],[6,7],[2,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The maximum number of envelopes you can Russian doll is <code>3</code> ([2,3] =&gt; [5,4] =&gt; [6,7]).
</pre>

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

<pre>
<strong>Input:</strong> envelopes = [[1,1],[1,1],[1,1]]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= envelopes.length &lt;= 10<sup>5</sup></code></li>
	<li><code>envelopes[i].length == 2</code></li>
	<li><code>1 &lt;= w<sub>i</sub>, h<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        sort(envelopes.begin(),envelopes.end(),[](auto lhs,auto rhs){
            if(lhs[0]==rhs[0]){
                return lhs[1]>rhs[1];
            } 
            return lhs[0]<rhs[0];
        });

        // return findLis(envelopes);
        return findLisBS(envelopes);
    }

    int findLisBS(vector<vector<int>>& envelopes){
        int n=envelopes.size();
        vector<int> sorted;
        sorted.push_back(envelopes[0][1]);
        for(int i=1;i<n;i++){
            auto itr=lower_bound(sorted.begin(),sorted.end(),envelopes[i][1]);
            if(itr==sorted.end()){
                sorted.push_back(envelopes[i][1]);
            } else {
                int index=itr-sorted.begin();
                sorted[index]=envelopes[i][1];
            }
        }
        return sorted.size();
    }

    // int findLis(vector<vector<int>>& envelopes){
    //     int n=envelopes.size();
    //     vector<int> dp(n);
    //     dp[n-1]=1;
    //     int ans=1;
    //     for(int i=n-2;i>=0;i--){
    //         int subAns=1;
    //         for(int j=i+1;j<n;j++){
    //             if(envelopes[j][1]>envelopes[i][1]){
    //                 subAns=max(subAns,dp[j]+1);
    //             }
    //         }
    //         dp[i]=subAns;
    //         ans=max(ans,subAns);
    //     }
    //     return ans;
    // }

    
};


// class Solution {
// public:
//     int maxEnvelopes(vector<vector<int>>& envelopes) {
//         sort(envelopes.begin(),envelopes.end(),[](auto lhs,auto rhs){
//             if(lhs[0]==rhs[0]){
//                 return lhs[1]<rhs[1];
//             } 
//             return lhs[0]>rhs[0];
//         });
//         unordered_map<int,unordered_map<int,vector<int>>> cache;
//         return solve(envelopes,0,INT_MAX,INT_MAX,cache);
//     }

//     int solve(vector<vector<int>>& envelopes,int index,int hMax,int wMax,unordered_map<int,unordered_map<int,vector<int>>>& cache){
//         if(index>=envelopes.size()){
//             return 0;
//         }

//         if(cache.find(hMax)!=cache.end() && cache[hMax].find(wMax)!=cache[hMax].end() && cache[hMax][wMax][index]!=-1){
//             return cache[hMax][wMax][index];
//         }

//         if(cache[hMax][wMax].size()==0){
//             cache[hMax][wMax].resize(envelopes.size()+1,-1);
//         }
//         int ans=0;
//         if(envelopes[index][0]<hMax && envelopes[index][1]<wMax)
//             ans=max(ans,1+solve(envelopes,index+1,min(hMax,envelopes[index][0]),min(wMax,envelopes[index][1]),cache));
//         ans=max(ans,solve(envelopes,index+1,hMax,wMax,cache));
//         return cache[hMax][wMax][index]=ans;
//     }
// };



/*
5   4
6   4
6   7


6   7
6   4
5   4


*/
353953