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

from bisect import bisect_left
class Solution:
    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
        envelopes.sort(key=lambda x: (x[0], -x[1])) #if two have the same width, so they shouldn't be considered compatible
        longest_increasing_subsequence = []
        for w, h in envelopes:
            idx = bisect_left(longest_increasing_subsequence, h)
            if idx == len(longest_increasing_subsequence):
                longest_increasing_subsequence.append(h)
            else:
                longest_increasing_subsequence[idx] = h
        return len(longest_increasing_subsequence)

#Time O(nlogn) sorting cost O(nlogn), longest increasing subsequence created using binary search O(nlogn)
#Space O(n) for longest_increasing_subsequence list

#find the length of longest increasing subsequence of heights using modified binary search
#The bisect_left function from the bisect module is used to find the position in a sorted list where an element should be inserted to keep the list sorted. It performs a binary search, which makes it efficient (O(logn)) for this purpose.

    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
        envelopes.sort(key = lambda x: (x[0], -x[1]))
        n = len(envelopes)
        ends = [0] * n
        length = 0

        def binary_search(ends, length, num):
            find = -1
            l, r = 0,  length
            while l <= r:
                m = l + (r - l)//2
                if ends[m] >= num:
                    find = m
                    r = m - 1
                else:
                    l = m + 1
            return find

        for i in range(n):
            num = envelopes[i][1]
            find = binary_search(ends, length, num)
            if find == -1:
                ends[length] = num
                length += 1
            else:
                ends[find] = num
        return length

#time O(nlogn)
62800