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.
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] => [5,4] => [6,7]).
Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1
The problem resembles the "Longest Increasing Subsequence" (LIS) problem. The main challenge is to properly define when one envelope can fit into another. An envelope (w1, h1) can fit into another envelope (w2, h2) if both w1 < w2 and h1 < h2.
Dynamic programming can be used by sorting the envelopes based on width, and then computing the LIS based on the height of envelopes such that previous envelopes' width and height are both less than the current one.
dp array.Leverage the fact that when width is sorted, the problem simplifies to finding the LIS based on height. By sorting the second criteria (height) in descending order when widths are equal, we ensure no two envelopes of the same width contribute to the LIS.
Use a more optimal approach with a binary search to find the correct position of each height in the constructed LIS.