We have a collection of envelopes, each defined by a width and a height. Envelope A fits inside envelope B only when A's width is strictly less than B's width AND A's height is strictly less than B's height. We want to find the longest chain of envelopes where each one nests inside the next.
This is a longest chain problem in two dimensions. If the envelopes only had one dimension, it would reduce to the classic Longest Increasing Subsequence (LIS). The complication is that we need both dimensions to be strictly increasing, and equal dimensions do not count.
If we can sort the envelopes so that the width condition is handled by the ordering itself, the problem reduces to a 1D LIS on the heights.
1 <= envelopes.length <= 10^5 → With up to 100,000 envelopes, O(n^2) dynamic programming (10^10 operations) will time out. We need O(n log n) or better.Treat this as a variant of Longest Increasing Subsequence in two dimensions. Sort the envelopes by width (breaking ties by height), then for each envelope, look at all previous envelopes and check whether the current one can contain them.
This is the same idea as the classic O(n^2) LIS: for each element, check all elements before it and extend the longest valid chain. The difference is that our "increasing" condition requires both width and height to be strictly increasing.
dp array where dp[i] represents the length of the longest chain ending with envelope i.dp[i] = 1 (each envelope is a chain of length 1 by itself).i, look at all previous envelopes j (where j < i). If envelopes[j] fits inside envelopes[i] (both width and height are strictly less), then dp[i] = max(dp[i], dp[j] + 1).dp array.This is too slow for n = 10^5. The next approach replaces the inner loop with a binary search by first sorting the envelopes so the width condition is satisfied by the ordering, reducing the problem to a 1D LIS on heights in O(n log n).
Sort envelopes by width ascending, so any left-to-right subsequence has non-decreasing widths. We still need strictly increasing widths and heights. For equal widths, sort heights descending. This prevents two same-width envelopes from both appearing in the LIS on heights. After sorting, the problem becomes LIS on the heights array, which we solve in O(n log n) using patience sorting (binary search with a tails array).
The descending height sort for equal widths is what makes the reduction valid. Consider a group of envelopes with the same width. Their heights appear in strictly decreasing order, so a strictly increasing subsequence of heights can pick at most one of them. That matches the rule that two same-width envelopes cannot nest. If we instead sorted heights ascending within equal widths, the LIS on heights could include several envelopes of the same width, overcounting the answer.
tails array where tails[i] is the smallest tail of any increasing subsequence of length i+1. For each height, binary search for the leftmost position where tails[pos] >= height. Replace or append accordingly.tails is the answer.