forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 06. Russian Doll Envelopes.md
- Loading branch information
1 parent
b42890d
commit 4da1067
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...ogramming/07. Longest Increasing Subsequence LIS/06. Russian Doll Envelopes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
### [354. Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/) | ||
|
||
**Prerequisite:** \ | ||
Before moving on to the solution, you should know how can we find the length of Longest Increasing Subsequence unsing Binary Search. You can find the detailed explanation of the logic on the below link. | ||
https://leetcode.com/problems/longest-increasing-subsequence/discuss/1636162/java-binary-search-stepwise-explanation | ||
|
||
**Why we need to sort?** \ | ||
In these types of problem when we are dealing with two dimensions, we need to reduce the problem from two-dimensional array into a one-dimensional array in order to improve time complexity. | ||
"**Sort first when things are undecided**", sorting can make the data orderly, reduce the degree of confusion, and often help us to sort out our thinking. the same is true with this question. Now, after doing the correct sorting, we just need to find Longest Increasing Subsequence of that one dimensional array. | ||
**Now, you may be wondered what correct sorting actually is?** | ||
It is the sorting which we do to order to achieve the answer. Like, increasing, non-increasing sorting. Without any further discussion, let's dig into Intuition followed by algorithm. | ||
|
||
**Algorithm** \ | ||
We sort the array in increasing order of width. And if two widths are same, we need to sort height in decreasing order. | ||
Now why we need to sort in decreasing order if two widths are same. By this practice, we're assuring that no width will get counted more than one time. Let's take an example | ||
envelopes=[[3, 5], [6, 7], [7, 13], [6, 10], [8, 4], [7, 11]] | ||
|
||
![image](https://user-images.githubusercontent.com/77569653/211143673-2a697d2a-6686-4c48-b9a8-12d00e598da6.png) | ||
|
||
Now, if you see for a while, **6 and 7** is counted twice while we're calculating the length of LIS, which will give the wrong ans. As question is asking, if any width/height are less than or equal, then, it is not possible to russian doll these envelopes. | ||
Now, we know the problem. So, how can we tackle these conditions when two width are same, so that it won't affect our answer. We can simple **reverse sort the height if two width are equal, to remove duplicacy**. | ||
Now, you may question, how reverse sorting the height would remove duplicacy? As the name itself says, Longest Increasing Subsequnce, the next coming height would be less than the previous one. Hence, forbidding it to increase length count. | ||
|
||
![image](https://user-images.githubusercontent.com/77569653/211143680-6aac3cb2-2cd8-4894-9701-9707152ea8a9.png) | ||
|
||
If you don't understand how LIS is calculated here, I strongly refer you to follow the prerequisite. | ||
Now, we have **sucessfully reduced the problem to LIS!** All you need to apply classical LIS on heights, to calculate the ans. This would be the maximum number of envelopes can be russian doll. | ||
|
||
**Code:** | ||
|
||
```python | ||
class Solution: | ||
def maxEnvelopes(self, envelopes): | ||
envelopes.sort(key=lambda x: (x[0], -x[1])) | ||
LIS = [] | ||
for (w, h) in envelopes: | ||
if not LIS or h > LIS[-1]: | ||
LIS.append(h) | ||
else: | ||
l, r = 0, len(LIS) | ||
while l < r: | ||
m = l + (r - l) // 2 | ||
if LIS[m] < h: | ||
l = m + 1 | ||
else: | ||
r = m | ||
LIS[l] = h | ||
|
||
return len(LIS) | ||
``` |