Russian Doll Envelopes
The problem of finding the maximum number of envelopes you can Russian doll is a more complicated version of the classic Longest Increasing Subsequence (LIS) problem.
Here’s how to approach the problem:
Sort the Envelopes: Sort the envelopes by width in ascending order. If two envelopes have the same width, sort them by height in descending order. This ensures that when comparing envelopes with the same width, the one with larger height will not be chosen, as it cannot fit inside another envelope with the same width.
Find the Longest Increasing Subsequence: After sorting, the problem is reduced to finding the LIS of the heights.
Below is the code implementing these steps:


Explanation:
 The
sort
method arranges the envelopes so that you can apply the LIS algorithm to the heights.  The
bisect_left
function from thebisect
module is used to find the position at which a height can be inserted in the LIS array. If the height can extend the current LIS, it is appended; otherwise, it replaces an existing element.  The length of the LIS array gives you the maximum number of envelopes you can Russian doll.
The time complexity of this approach is (O(N \log N)), where (N) is the number of envelopes.
from bisect import bisect_left


Identifying Problem Isomorphism
“Russian Doll Envelopes” asks for the maximum number of envelopes you can Russian doll, meaning to put one inside another, based on width and height.
An isomorphic problem to this is “Box Stacking” (not directly on LeetCode but a common problem in the category of dynamic programming). This problem asks you to find the highest possible stack of boxes such that for every box, all boxes below it have a larger width, height, and depth.
The reason these problems map to each other is that both involve finding the longest increasing subsequence in a 2D space (width and height in the envelopes problem, width, height, and depth in the box stacking problem).
“Box Stacking” is more complex than “Russian Doll Envelopes”. While the “Russian Doll Envelopes” problem involves finding the maximum number of envelopes that can be put into another based on two dimensions, “Box Stacking” involves the additional dimension of depth.
“Russian Doll Envelopes” is simpler as it involves only two dimensions (width and height) and requires only two types of comparisons (whether one envelope’s width and height are both smaller than the other’s). On the other hand, “Box Stacking” involves three dimensions (width, height, and depth), making it more complex due to the additional type of comparison needed.
If there were multiple isomorphic problems, “Russian Doll Envelopes” would be the simpler one, followed by “Box Stacking”.
“Box Stacking” is not listed on LeetCode, but it is a common dynamic programming problem that asks for finding the highest possible stack of boxes such that for every box, all boxes below it have a larger width, height, and depth.
An isomorphic problem to “Box Stacking” on LeetCode is “Maximum Length of Pair Chain” (#646). In this problem, you’re given n pairs where the first element of every pair is strictly less than the second element, and you need to find the longest chain which can be formed such that for every pair (i, j) in the chain, i < j.
The reason for this mapping is that both problems involve finding the longest sequence based on a certain condition. In “Box Stacking”, the condition is that the width, height, and depth of each box in the stack must be smaller than the box beneath it. In “Maximum Length of Pair Chain”, the condition is that the first element of each pair must be smaller than the second element of the preceding pair.
“Box Stacking” is more complex than “Maximum Length of Pair Chain”. This is because “Box Stacking” requires dealing with three dimensions (width, height, and depth), whereas “Maximum Length of Pair Chain” only involves pairs (two dimensions).
If there were multiple isomorphic problems, “Maximum Length of Pair Chain” would be the simpler one, followed by “Box Stacking”.
10 Prerequisite LeetCode Problems
“354. Russian Doll Envelopes” is a problem of sorting and dynamic programming. Here are 10 problems to build up the skills needed for solving this problem:
“300. Longest Increasing Subsequence”
 This problem is crucial as “Russian Doll Envelopes” is essentially a variant of this problem. The primary difference is that you have an extra dimension to consider.
“198. House Robber”
 This problem also uses dynamic programming and helps you think about optimal sequences.
“673. Number of Longest Increasing Subsequence”
 This problem is a good continuation after the “Longest Increasing Subsequence”. It includes the same concepts, but adds some additional complexity.
“646. Maximum Length of Pair Chain”
 This problem is very similar to “Russian Doll Envelopes”, but a bit easier. It’s a good problem to solve before tackling the main one.
“70. Climbing Stairs”
 This is a basic dynamic programming problem and can help you understand the basics of this type of problem.
“322. Coin Change”
 Another classic dynamic programming problem. It will help you get comfortable with this type of problem.
“215. Kth Largest Element in an Array”
 This problem can help you understand sorting and its importance in simplifying problems.
“34. Find First and Last Position of Element in Sorted Array”
 Understanding how to handle sorted arrays is a key to solving the “Russian Doll Envelopes” problem.
“128. Longest Consecutive Sequence”
 This problem is another one that revolves around ordering and sequence.
“435. Nonoverlapping Intervals”
 Understanding how to handle and compare intervals is important in the “Russian Doll Envelopes” problem.


Problem Classification
The task is to find the maximum number of envelopes that can be put one inside another. This is a Nested Object Maximization Problem.
Language Agnostic Coding Drills
The code is implementing a variant of the Longest Increasing Subsequence (LIS) problem. It first sorts the ’envelopes’ list and then uses a binary search algorithm to find the longest subsequence. Here are the key concepts involved in solving this problem:
Sorting a List of Lists: The first concept you need to understand is how to sort a list of lists. In this problem, you’re not just sorting based on the first element of each sublist but also considering the second element.
Sorting with a Custom Comparator: This builds upon the previous concept but introduces the idea of a custom sorting function. In Python, you can pass a lambda function as the ‘key’ argument to the sort function to control how the list is sorted.
Understanding the Longest Increasing Subsequence (LIS) Problem: This is a wellknown problem in computer science, and understanding it is key to understanding the code above. The problem asks for the length of the longest subsequence of a given sequence in which the subsequence’s elements are in sorted order.
Solving the LIS Problem with Dynamic Programming: A common approach to solving the LIS problem is to use dynamic programming, a method for solving problems by breaking them down into smaller, simpler subproblems and combining their solutions to solve the larger, original problem.
Solving the LIS Problem with Binary Search: An alternative approach to solving the LIS problem is to use binary search. This approach involves maintaining an active list of potential subsequences and using binary search to update the list as you iterate through the input sequence. This method tends to be more efficient than the dynamic programming approach.
Understanding the bisect_left Function: The bisect_left function is a powerful tool in Python for performing binary searches. Understanding how it works and when to use it is key to understanding the code above.
Applying the LIS Problem to the ‘Max Envelopes’ Problem: The final step is to understand how the LIS problem applies to the ‘Max Envelopes’ problem. Once you understand the LIS problem and how to solve it, it’s a matter of translating the envelopes into a sequence of numbers and applying the LIS algorithm.
Targeted Drills in Python
Let’s write code snippets for each concept to understand how they work:
 Sorting a List of Lists:


 Sorting with a Custom Comparator:


Understanding the Longest Increasing Subsequence (LIS) Problem:
 This concept is better understood theoretically, so no Python code here.
Solving the LIS Problem with Dynamic Programming:


 Solving the LIS Problem with Binary Search:


 Understanding the bisect_left Function:


 Applying the LIS Problem to the ‘Max Envelopes’ Problem:
 This is your final solution, so this will be applying all the above drills together:

