Skip to main content
LeetCode 354, Hard. Topics: Array, Binary Search, Dynamic Programming, Sorting, Longest Increasing Subsequence. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope. One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope’s width and height. Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other). Note: You cannot rotate an envelope.

Examples

Constraints

  • 1 <= envelopes.length <= 10<sup>5</sup>
  • envelopes[i].length == 2
  • 1 <= w<sub>i</sub>, h<sub>i</sub> <= 10<sup>5</sup>

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026