Solution: Russian Doll Envelopes
Let’s solve the Russian Doll Envelopes problem using the Sort and Search pattern.
We'll cover the following
Statement
You are given a 2D array of integers, envelopes
, where each element envelopes[i] = [w
i
, h
i
]
represents the width and height of an envelope. An envelope can fit inside another if and only if its width and height are strictly smaller than the width and height of the other envelope. The task is to determine the maximum number of envelopes that can be nested inside each other, similar to
Constraints:
envelopes.length
envelopes[i].length
w
i
,h
i
Solution
This solution uses a combination of sorting and searching techniques to determine the maximum number of envelopes that can be nested within each other, keeping in light that both the width and height of the envelope to fit must be smaller than the other envelope. The challenge in this problem is to simultaneously compare dimensions, width, and height while keeping the solution right. This is handled by sorting the envelopes by width in ascending order and, in the case of ties, sorting by height in descending order. This descending height is important because it prevents misunderstanding of envelopes with the same width. Smaller envelopes could incorrectly nest within larger ones if heights are sorted in ascending order.
Once the envelopes are sorted, the solution finds the l
Binary search determines the position in LIS where the current height can fit. The idea is to find the first height in LIS greater than or equal to the current height. This ensures the sequence remains sorted and allows the replacement of larger values, maintaining the smallest possible subsequences for future comparisons.
If no such position exists (i.e., the height is greater than all values in LIS), the height is appended to LIS, meaning an extension of the LIS.
If a valid position exists, the height replaces the value at that position in LIS, ensuring that subsequences of that length have the smallest possible ending height.
Here’s the step-by-step implementation of the solution:
Sort the
envelopes
by width in ascending order, and in case of conflicting widths, sort those envelopes by height in descending order.Initialize an empty array,
lis
, to store the heights of envelopes in increasing order. It will track the envelopes based on height.For each envelope’s
width
andheight
:Use the helper function,
find_position
, to find the correctposition
to insert aheight
inlis
while maintaining order.If the
position
equals the length oflis
, append theheight
tolis
as the height can extend the sequence.Otherwise, replace the
height
at theposition
inlis
to keep the sequence as small as possible while maintaining the subsequence.
After processing all envelopes, the length of the
lis
gives the maximum number of envelopes that can be Russian dolled.
The helper function, find_position
, uses binary search to find the position where the height of the current envelope will be inserted in the lis
. It takes the lis
array and the height
of the current envelope as parameters and performs the following steps:
Initialize
left
andright
at the starting and ending indexes of thelis
array, respectively.Perform binary search until
left
exceedsright
:Calculate the midpoint,
mid
, betweenleft
andright
.If the value at
mid
is smaller than theheight
, move theleft
tomid + 1
to search the right half.Otherwise, adjust the
right
tomid-1
to search the left half.
Finally, the
left
gives the position where theheight
can fit.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.