Russian Doll Envelopes

Given envelopes with width and height, determine the maximum number of envelopes you can nest one inside another. A custom sort by width ascending and height descending is required before applying a longest increasing subsequence step.

Input Format

envelopes = array of [width, height]

Output Format

maximum number of nested envelopes

Constraints

  • 1 <= envelopes.length <= 10^5; envelopes[i].length == 2; 1 <= width, height <= 10^5

Examples

Example 1:

Input:

envelopes = [[5,4],[6,4],[6,7],[2,3]]

Output:

3

Explanation:

One optimal chain is [2,3] -> [5,4] -> [6,7].

Example 2:

Input:

envelopes = [[1,1],[1,1],[1,1]]

Output:

1

Explanation:

Equal envelopes cannot nest inside each other.

Loading...
Russian Doll Envelopes