 New update is available. Click here to update.

# Russian Doll Envelopes

Last Updated: 15 Jan, 2021
Difficulty: Hard

## PROBLEM STATEMENT

#### What is the maximum number of envelopes you can Russian doll? (put one inside other)

##### Note
``````Rotation of envelope is not allowed, that is, height and width can’t be exchanged
``````
##### Input Format:
``````The first line of input contains an integer ‘T’ denoting the number of test cases.

The next 3*T lines represent the ‘T’ test cases.

The first line of each test case contains an integer ‘N’, representing the number of envelopes.

The second line of the test case contains ‘N’ space-separated integers representing elements of the array ‘height’.

The third line of the test case contains ‘N’ space-separated integers representing elements of the array ‘width’.
``````
##### Output Format :
``````For each test case, print, in a separate line, the maximum number of envelopes you can Russian doll.
``````

#### Note:

``````You do not need to print anything, it has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 50
1 <= n <= 10^4
1 <= height[i] <= 10^9
1 <= width[i] <= 10^9

Time Limit: 2 sec
`````` ## Approach 1

• This is a recursive approach.
• Initialize two integer variables ‘maxHeight’:= INF,  ‘maxWidth’ := INF, where INF denotes infinity. These variables will keep the maximum height and width of the envelope that can be placed inside.
• Recursively try out all possible ways of Russian doll envelopes and find out the maximum number of the envelopes that can Russian Doll. This can be done by performing the following step in each recursive call -:
• Initialize an integer variable ‘maxEnvelope’ := 0.
• Iterate over all ‘n’ envelopes in each recursive call and if the width of the envelope is strictly less than ‘maxWidth’ and the height of the envelope is strictly less than ‘maxHeight’ then try to place this envelope inside by making a recursive call with ‘maxWidth’ and ‘maxHeight’  as width and height of current envelope respectively. Update value of ‘maxEnvelope’ with a maximum of its current value and 1 + value returned in that recursive call.
• Return ‘maxEnvelope’.
• Return the value obtained by recursive function, It will be the maximum number of envelopes that can Russian Doll.