Russian Doll Envelopes
LeetCode 354 · Hard · solution, intuition & step-by-step walkthrough
Sort (w ↑, h ↓), then LIS on heights.
This is how I solved Russian Doll Envelopes (LeetCode 354), a hard problem from the LIS pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.
Intuition
Sort by width so any nesting chain moves left to right; then you only need heights to strictly increase — an LIS. The trick: for equal widths, sort heights descending so two envelopes with the same width can never both be picked by the LIS.
Approach
- 1
First attempt: memo on (index, previous index) — correct but O(n²) states, too slow for large inputs.
- 2
Sort envelopes by width ascending and height descending.
- 3
Extract the heights and run the O(n log n) patience LIS.
- 4
The LIS length is the number of envelopes you can nest.
How I solved it · from first idea to final
lru_cache dfs(index, prev_index).
Sort trick turns 2-D into 1-D LIS.
Pick / skip + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
envelopes.sort()
@lru_cache(maxsize=None)
def dfs(index,prev_index):
if index==len(envelopes):
return 0
pick = 0
if prev_index==-1 or (envelopes[index][0]>envelopes[prev_index][0] and envelopes[index][1]>envelopes[prev_index][1]):
pick=1+dfs(index+1,index)
dont_pick=dfs(index+1,prev_index)
return max(pick,dont_pick)
return dfs(0,-1)Sort + LIS (bisect)PythonO(n log n) time · O(n) space
from bisect import bisect_left
class Solution:
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
#sort after that build LIS
envelopes.sort(key=lambda x:(x[0],-x[1]))
def lis(nums):
subs=[nums[0]]
for num in nums:
if num>subs[-1]:
subs.append(num)
else:
k = bisect_left(subs,num)
subs[k]=num
return len(subs)
return lis([i[1] for i in envelopes])Russian Doll Envelopes — FAQ
What is the intuition behind Russian Doll Envelopes?
Sort by width so any nesting chain moves left to right; then you only need heights to strictly increase — an LIS. The trick: for equal widths, sort heights descending so two envelopes with the same width can never both be picked by the LIS.
How do you solve Russian Doll Envelopes (LeetCode 354)?
1. First attempt: memo on (index, previous index) — correct but O(n²) states, too slow for large inputs. 2. Sort envelopes by width ascending and height descending. 3. Extract the heights and run the O(n log n) patience LIS. 4. The LIS length is the number of envelopes you can nest.
What is the time and space complexity of Russian Doll Envelopes?
Time: O(n log n). Space: O(n). (Accepted approach: Sort + LIS (bisect).)
What are common mistakes in Russian Doll Envelopes?
Without height-descending on ties, [3,4] and [3,5] would wrongly nest. Both width and height must be strictly larger.
Is there more than one way to solve Russian Doll Envelopes?
Pick / skip + memo (O(n²) time · O(n²) space): lru_cache dfs(index, prev_index). Sort + LIS (bisect) (O(n log n) time · O(n) space): Sort trick turns 2-D into 1-D LIS.