<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(),envelops.end());
        vector <pair<int,int>> b(envelopes.size());
        for(int i=0;i<envelopes.size();i++)
        {
            b[i].first=envelopes[i][0];
            b[i].second=-envelopes[i][1];
        }
        sort(b.begin(),b.end());
        vector <pair<int,int>> a;
        for(int i=0;i<b.size();i++)
        {
            a.push_back({b[i].first,-b[i].second});
            // if(i==0)
            // {
            //     a.push_back({b[i].first,-b[i].second});
            //     cout<<b[i].first<<" "<<-b[i].second<<endl;
            // }
            // else if(i>0)
            // {
            //     if(b[i].first!=b[i-1].first)
            //     {
            //         a.push_back({b[i].first,-b[i].second});
            //         cout<<b[i].first<<" "<<-b[i].second<<endl;
            //     }
            // }
        }
        int ans=0;
        set <pair<int,int>> s;
        int n=a.size();
        for(int i=0;i<n;i++)
        {
            int v=a[i].second;
            if(i==0)
            {
                ans=max(ans,1);
                s.insert({v,1});
            }
            else
            {
                auto it=s.lower_bound({v,0});
                
                if(it==s.begin())
                {
                    int cv=1;
                    vector <pair<int,int>> er;
                    for(;it!=s.end();it++)
                    {
                        pair <int,int> p=*it;
                        if(p.second<=cv)
                        {
                            er.push_back(p);
                        }
                        else
                        {
                            break;
                        }
                    }
                    for(int j=0;j<er.size();j++)
                    {
                        s.erase(er[j]);
                    }
                    s.insert({v,cv});
                    ans=max(ans,cv);
                }
                else
                {
                    auto it2=it;
                    it2--;
                    int cv=(*it2).second+1;
                    // cout<<(*it2).first<<" "<<(*it2).second<<endl;
                    vector <pair<int,int>> er;
                    for(;it!=s.end();it++)
                    {
                        pair <int,int> p=*it;
                        if(p.second<=cv)
                        {
                            er.push_back(p);
                        }
                        else
                        {
                            break;
                        }
                    }
                    for(int j=0;j<er.size();j++)
                    {
                        s.erase(er[j]);
                    }
                    s.insert({v,cv});
                    ans=max(ans,cv);
                }
            }
        }
        return ans;
    }
};
113371