Imagine you have a collection of envelopes and you want to nest them inside each other like Russian dolls! π
You are given a 2D array envelopes where envelopes[i] = [width, height] represents the dimensions of each envelope. An envelope can fit inside another envelope if and only if both its width and height are strictly smaller than the outer envelope.
Your goal: Find the maximum number of envelopes you can nest inside each other. Note that you cannot rotate the envelopes - they must maintain their original orientation.
Example: Given envelopes [[5,4],[6,4],[6,7],[2,3]], you can nest them as: [2,3] β [5,4] β [6,7] for a maximum of 3 envelopes.
Input & Output
Visualization
Time & Space Complexity
O(n log n) for sorting + O(n log n) for LIS with binary search
Array to store the LIS sequence
Constraints
- 1 β€ envelopes.length β€ 105
- envelopes[i].length == 2
- 1 β€ widthi, heighti β€ 105
- Cannot rotate envelopes - must maintain original orientation