<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>

struct envelope{
    int width;
    int height;
};
class Solution {
public:
    int findCeil(vector<int>heights,int height){
        int l =0,r= heights.size()-1;
        while(l<r){
            int mid =l+(r-l)/2;
            if(heights[mid]>=height){
                r= mid;
            }
            else{
                 l=mid+1;
            }
        }
        return r;

    }
    static bool com(envelope a, envelope b){
          if(a.width!=b.width)return a.width<b.width;
          else return a.height>b.height;
    }
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        int n  = envelopes.size();
        if(n==0)return 0;
        envelope arr[n];
        for(int i=0;i<envelopes.size();i++){
            arr[i].width = envelopes[i][0];
            arr[i].height = envelopes[i][1];
        }
        sort(arr,arr+n,com);
        vector<int>heights;
        heights.push_back(arr[0].height);
        int l =1;
        for(int i=1;i<n;i++){
            if(arr[i].height>heights[l-1]){
                heights.push_back(arr[i].height);
                l++;
            }
            else{
                int ceil = findCeil(heights,arr[i].height);
                heights[ceil] = arr[i].height; 
            }
        }
       
        return heights.size();
    }
};
231103