Russian Doll Envelopes LeetCode

The Russian Doll Envelopes problem is an intriguing computer programming and problem-solving puzzle. The task is to find the maximum number of envelopes you can nest inside one another. However, there’s a twist: we can only nest an envelope inside another if its width and height are strictly greater than the width and height of the outer envelope.

The Russian Doll Envelopes problem is not just a puzzle but a rich learning opportunity that enhances critical skills in computational thinking and algorithm design. By solving this problem, learners will develop a strong understanding of dynamic programming and sorting algorithms, particularly the concept of nested sequences. These skills are directly applicable in real-world scenarios where optimal arrangements or hierarchies are needed, such as scheduling tasks, optimizing storage, or organizing resources in complex systems.

This Answer will provide a coding solution for this problem alongside a quiz through which we can test our concepts.

Problem statement

In this problem, we’re provided with a2D2Darray of integers envelopes where envelopes[i] = [wi, hi] represents the width (wi) and height (hi) of an envelope.

An envelope can be nested in another if its width and height are larger than the width and height of the other envelope. Our task is to determine the maximum number of envelopes that can be nested inside each other, similar to Russian nesting dolls.

Constraints:

  • 11 \leqenvelopes.length 1000\leq 1000

  • envelopes[i].length ==2== 2

  • 1wi,hi1041\leq w_i, h_i \leq 10^4

Let’s take a look at a few examples to get a better understanding of the problem statement:

canvasAnimation-image
1 of 2

Solution

To solve the problem of determining the maximum number of envelopes that can be nested inside each other, we can follow these steps:

  1. Check if the input list envelopes is empty. If it is, return 0, indicating no envelopes to nest.

  2. Sort the envelopes in ascending order based on their heights.

  3. Initialize an array, maxvalues, to store the maximum number of nested envelopes up to each envelope. Each element of maxvalues represents the maximum number of envelopes nested up to the corresponding envelope in the sorted list.

  4. Initialize each element of maxvalues with 1, indicating that each envelope can be nested in itself.

  5. Iterate over each envelope curr in the sorted list of envelopes. For each envelope curr, iterate over all the previous envelopes (temp) in the list (from index 0 to i).

  6. Compare the width and height of the current envelope curr with the width and height of the previous envelope temp.

  7. If both the width and height of curr are greater than the width and height of temp, update the count of nested envelopes for curr to be one more than the current count. Return the maximum value.

Russian dolls example
Russian dolls example

As we can see, the smallest doll can fit inside the other ones as both its height and width are less than the one it’s going to be enveloped in.

Educative 99 helps you solve complex coding problems like the Russian doll envelopes problem by teaching you to recognize patterns and apply the right algorithms.

Let’s look at the code for the algorithm we just discussed.

def max_envelopes(envelopes):
if len(envelopes) == 0:
return 0
# Sort the envelopes by height in ascending order
envelopes.sort(key=lambda env: env[1])
# Initialize the maxvalues array to store the maximum number of nested envelopes
maxvalues = [1 for env in envelopes]
for i in range(0, len(envelopes)):
curr = envelopes[i]
# Compare the current envelope with all previous envelopes
for j in range(0, i):
temp = envelopes[j]
# Check if the current envelope can nest within the previous envelope
if curr[0] > temp[0] and curr[1] > temp[1]:
# Update the nested count if a valid nesting is found
if maxvalues[j] + 1 > maxvalues[i]:
maxvalues[i] = maxvalues[j] + 1
return max(maxvalues)
# Driver code
def main():
envelopes = [[[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]],
[[1,1],[1,1],[1,1]],
[[4,5],[4,6],[6,7],[2,3],[1,1]]]
for i in range(len(envelopes)):
print(i+1, '.', '\tGiven envelops: ', envelopes[i], sep='')
result = max_envelopes(envelopes[i])
print('\n\tMaximum number of nested envelopes: ', result)
print('-' * 100)
if __name__ == '__main__':
main()
Russian Doll Envelopes

After discussing the solution to the given problem, we will now discuss its complexity analysis.

Time complexity

The time complexity of the solution is O(n2)O(n^2), primarily due to the nested loops used in the dynamic programming approach.

Space complexity

The space complexity is O(n)O(n), wherennis the number of envelopes.

Knowledge test

Solve the quiz below to test your understanding of this problem:

Russian Doll Envelopes LeetCode

1

What does the 2D array, envelopes, represent in this problem?

A)

A list of Russian nesting dolls

B)

A list of envelope widths and heights

C)

A list of random integers

D)

None of the above

Question 1 of 40 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved