<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>>& e) {
        vector<pair<int, int>> envelopes(e.size());
        for(int i=0;i<e.size();i++)envelopes[i] = {e[i][0], e[i][1]};
        sort(envelopes.begin(), envelopes.end(), [](pair<int, int>& a, pair<int, int>& b){
            if(a.first == b.first)return a.second < b.second;
            return a.first > b.first;
        });
        set<pair<int, int>> mySet;
        priority_queue<pair<int, int>> pq;
        for(int i=0;i<envelopes.size();i++)pq.push({envelopes[i].second, i});
        while(!pq.empty()){
            auto p = pq.top();
            pq.pop();
            auto it = mySet.lower_bound({p.second, 0});
            if(it == mySet.begin()){
                mySet.insert({p.second, 1});
                continue;
            }
            int newValue = 0;
            it--;
            newValue = (*it).second + 1;
            auto next = it;
            next++;
            while(next != mySet.end() && (*next).second <= newValue){
                mySet.erase(next);
                next = it;
                next++;
            }
            mySet.insert({p.second, newValue});
        }
        return (*mySet.rbegin()).second;
    }
};
108253