--- comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md tags: - Array - Binary Search - Dynamic Programming - Sorting --- # [354. Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes) [中文文档](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md) ## Description

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.

 

Example 1:

Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

Example 2:

Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1

 

Constraints:

## Solutions ### Solution 1 #### Python3 ```python class Solution: def maxEnvelopes(self, envelopes: List[List[int]]) -> int: envelopes.sort(key=lambda x: (x[0], -x[1])) d = [envelopes[0][1]] for _, h in envelopes[1:]: if h > d[-1]: d.append(h) else: idx = bisect_left(d, h) d[idx] = h return len(d) ``` #### Java ```java class Solution { public int maxEnvelopes(int[][] envelopes) { Arrays.sort(envelopes, (a, b) -> { return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]; }); int n = envelopes.length; int[] d = new int[n + 1]; d[1] = envelopes[0][1]; int size = 1; for (int i = 1; i < n; ++i) { int x = envelopes[i][1]; if (x > d[size]) { d[++size] = x; } else { int left = 1, right = size; while (left < right) { int mid = (left + right) >> 1; if (d[mid] >= x) { right = mid; } else { left = mid + 1; } } int p = d[left] >= x ? left : 1; d[p] = x; } } return size; } } ``` #### C++ ```cpp class Solution { public: int maxEnvelopes(vector>& envelopes) { sort(envelopes.begin(), envelopes.end(), [](const auto& e1, const auto& e2) { return e1[0] < e2[0] || (e1[0] == e2[0] && e1[1] > e2[1]); }); int n = envelopes.size(); vector d{envelopes[0][1]}; for (int i = 1; i < n; ++i) { int x = envelopes[i][1]; if (x > d[d.size() - 1]) d.push_back(x); else { int idx = lower_bound(d.begin(), d.end(), x) - d.begin(); if (idx == d.size()) idx = 0; d[idx] = x; } } return d.size(); } }; ``` #### Go ```go func maxEnvelopes(envelopes [][]int) int { sort.Slice(envelopes, func(i, j int) bool { if envelopes[i][0] != envelopes[j][0] { return envelopes[i][0] < envelopes[j][0] } return envelopes[j][1] < envelopes[i][1] }) n := len(envelopes) d := make([]int, n+1) d[1] = envelopes[0][1] size := 1 for _, e := range envelopes[1:] { x := e[1] if x > d[size] { size++ d[size] = x } else { left, right := 1, size for left < right { mid := (left + right) >> 1 if d[mid] >= x { right = mid } else { left = mid + 1 } } if d[left] < x { left = 1 } d[left] = x } } return size } ```