354. Russian Doll Envelopes
Asked at Atlassian
Difficulty
Hard
Time
O(n log n)
Space
O(n)
Pattern
Array
Companies
1
Problem
You are given a list of envelopes. Each envelope is a pair [width, height].
One envelope can be placed inside another only if both its width and its height are strictly smaller. So [2,3] fits inside [5,4], but [5,4] does not fit inside [6,4] because the heights are equal. You may not rotate an envelope.
Return the largest number of envelopes you can stack this way, one inside the next.
Nesting needs strict growth in both sides
Constraints
- ●
1 <= envelopes.length <= 10^5 - ●
envelopes[i].length == 2 - ●
1 <= wi, hi <= 10^5
Examples
envelopes = [[5,4],[6,4],[6,7],[2,3]]3envelopes = [[1,1],[1,1],[1,1]]1Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Atlassian | Hard | View all Atlassian questions → |
How to Think About It
Brute force: try all pairs and check if one fits inside another, O(n^3) DP.
Sort envelopes by width ascending, then reduce to longest increasing subsequence on heights.
Problem: simple LIS on heights allows equal heights, but we need strict inequality on both dimensions.
Sort by width ascending and by height descending for same widths, then LIS on heights avoids width ties.
Optimal: sort envelopes by width ascending then height descending, find LIS on heights using patience sorting with binary search in O(n log n).
Optimal Approach
Sort envelopes by width ascending, and for envelopes with the same width, sort by height descending. This ensures that when we compute the longest increasing subsequence on heights, no two envelopes with the same width can both be selected (since their heights are in descending order). After sorting, the problem reduces to finding the LIS of the height sequence using patience sorting with binary search, which runs in O(n log n) time. The length of this LIS is the maximum number of nested envelopes.
Sort, then LIS on heights
What Trips People Up in Real Interviews
Clarify that both width AND height must be strictly smaller for one envelope to fit inside another.
Explain why sorting by width ascending alone is insufficient due to ties.
Mention the trick of sorting heights descending within the same width to prevent selecting two envelopes with the same width.
Discuss the patience sorting approach for LIS in O(n log n).
Address edge cases like empty input or single envelope.
Solution Code
O(n log n) timeimport bisect
class Solution:
def maxEnvelopes(self, envelopes: list[list[int]]) -> int:
envelopes.sort(key=lambda x: (x[0], -x[1]))
heights = [h for _, h in envelopes]
tails = []
for h in heights:
pos = bisect.bisect_left(tails, h)
if pos == len(tails):
tails.append(h)
else:
tails[pos] = h
return len(tails)Frequently Asked Questions
What is the Russian Doll Envelopes problem?
Given a 2D array of envelopes where each envelope is [width, height], find the maximum number of envelopes you can Russian doll (one fits inside another). Envelope A fits inside B if and only if A.width < B.width and A.height < B.height.
How do you solve Russian Doll Envelopes?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Russian Doll Envelopes?
Russian Doll Envelopes is asked at Atlassian. It is a hard difficulty problem.
What are common mistakes on Russian Doll Envelopes?
- Clarify that both width AND height must be strictly smaller for one envelope to fit inside another.
- Explain why sorting by width ascending alone is insufficient due to ties.
- Mention the trick of sorting heights descending within the same width to prevent selecting two envelopes with the same width.
- Discuss the patience sorting approach for LIS in O(n log n).
- Address edge cases like empty input or single envelope.