Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
1 <= envelopes.length <= 105envelopes[i].length == 21 <= wi, hi <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Dynamic Programming
[[5,4],[6,4],[6,7],[2,3]]
[[1,1],[1,1],[1,1]]
longest-increasing-subsequence)the-number-of-weak-characters-in-the-game)longest-non-decreasing-subarray-from-two-arrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #354: Russian Doll Envelopes
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;
}
}
// Accepted solution for LeetCode #354: Russian Doll Envelopes
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
}
# Accepted solution for LeetCode #354: Russian Doll Envelopes
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)
// Accepted solution for LeetCode #354: Russian Doll Envelopes
struct Solution;
use std::cmp::Reverse;
impl Solution {
fn max_envelopes(mut envelopes: Vec<Vec<i32>>) -> i32 {
let n = envelopes.len();
envelopes.sort_unstable_by_key(|v| (v[0], Reverse(v[1])));
let mut dp = vec![];
for i in 0..n {
let height = envelopes[i][1];
if let Err(p) = dp.binary_search(&height) {
if p == dp.len() {
dp.push(height);
} else {
dp[p] = height;
}
}
}
dp.len() as i32
}
}
#[test]
fn test() {
let envelopes = vec_vec_i32![[5, 4], [6, 4], [6, 7], [2, 3]];
let res = 3;
assert_eq!(Solution::max_envelopes(envelopes), res);
let envelopes = vec_vec_i32![[4, 5], [4, 6], [6, 7], [2, 3], [1, 1]];
let res = 4;
assert_eq!(Solution::max_envelopes(envelopes), res);
}
// Accepted solution for LeetCode #354: Russian Doll Envelopes
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #354: Russian Doll Envelopes
// 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;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.