You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.

One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.

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

Note: You cannot rotate an envelope.

&nbsp;
Example 1:

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


Example 2:

Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1


&nbsp;
Constraints:


	1 &lt;= envelopes.length &lt;= 105
	envelopes[i].length == 2
	1 &lt;= wi, hi &lt;= 105


from bisect import bisect_left
class Solution:
    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
        envelopes = sorted(envelopes, key= lambda x:(x[0],-x[1]))
        rst = []
        for _,h in envelopes:
            i = bisect_left(rst,h)
            if i == len(rst):
                rst.append(h)
            else:
                rst[i] = h
        return len(rst)
class Solution {
    public int maxEnvelopes(int[][] envelopes) {
		//sort the envelopes considering only width
        Arrays.sort(envelopes, new sortEnvelopes());
		
		//Now this is a Longest Increasing Subsequence problem on heights
		//tempList to store the temporary elements, size of this list will be the length of LIS 
        ArrayList<Integer> tempList = new ArrayList<>();
        tempList.add(envelopes[0][1]);
		
        for(int i=1; i<envelopes.length; i++){
            if(envelopes[i][1]>tempList.get(tempList.size()-1)){
                tempList.add(envelopes[i][1]);
            } else{
			//if the element is smaller than the largest(last because it is sorted) element of tempList, replace the largest smaller element of tempList with it..
			//ex->(assume if envelopes[i][1] is 4), then >>[1,7,8] will become [1,4,8]<<
                int index = lowerBound(tempList, envelopes[i][1]);
                tempList.set(index, envelopes[i][1]);
            }
        }
        return tempList.size();
    }
    
	//finding the index of greatest smaller element 
    public int lowerBound(ArrayList<Integer> list, int search){
        int start = 0;
        int end = list.size()-1;
        while(start<end){
            int mid = start + (end-start)/2;
            if(list.get(mid) < search){
                start = mid+1;
            } else{
                end = mid;
            }
        }
        return start;
    }
}

class sortEnvelopes implements Comparator<int[]> {
    public int compare(int[] a, int[] b){
        if(a[0] == b[0]){
		//to ignore the duplicates, we are sorting such that, for same width-> element with 
		//largest height would be considered first, in this way all the other smaller heights would
		//be ignored
            return b[1] - a[1];
        } else{
            return a[0] - b[0];
        }
    }
}
class Solution {
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        int n = envelopes.size();
        sort(envelopes.begin(), envelopes.end(), [](auto &l, auto &r)
             {
                return l[0] == r[0] ? l[1] > r[1] : l[0] < r[0];
             });
        int len = 0;
        for(auto& cur: envelopes)
        {
            if(len==0 || envelopes[len-1][1] < cur[1])
                envelopes[len++] = cur;
            else
                *lower_bound(envelopes.begin(), envelopes.begin()+ len, cur, [](auto &l, auto &r)
                             {
                                 return l[1] < r[1];
                             }) = cur;
        }
        return len;
    }
};
const binarySearch = (arr, target) => {
    let left = 0;
    let right = arr.length - 1;
    
    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        if (arr[mid] === target) {
            return mid;
        }
        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return left;
}

var maxEnvelopes = function(envelopes) {
    envelopes.sort((a, b) => a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]);
    const sub = [envelopes[0][1]];
    
    for (let envelope of envelopes) {
        if (envelope[1] > sub[sub.length - 1]) {
            sub.push(envelope[1]);
        } else {
            const replaceIndex = binarySearch(sub, envelope[1]);
            sub[replaceIndex] = envelope[1];
        }
    }
    
    return sub.length;
};
Russian Doll Envelopes