AlgoFrames

Russian doll envelopes

Challenge · Write code · about 25 min

Each envelope is [width, height]. One envelope fits inside another only when both its width and its height are strictly smaller. Return the largest number of envelopes that can be nested one inside the next. Rotating is not allowed.

Example 1

Input:

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

Output:

3

[2, 3] fits in [5, 4], which fits in [6, 7].

Example 2

Input:

[[[1,1],[1,1],[1,1]]]

Output:

1

Equal envelopes do not fit inside each other.

Constraints

  • 1 ≤ envelopes.length ≤ 100000
  • 1 ≤ width, height ≤ 100000

Learn the idea first: Longest increasing subsequence; the n log n version, a visual lesson.

More Longest increasing subsequence; the n log n version problems