|
| 1 | +# Russian Doll Envelopes |
| 2 | + |
| 3 | +## Difficulty |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope. |
| 10 | + |
| 11 | +One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height. |
| 12 | + |
| 13 | +Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other). |
| 14 | + |
| 15 | +Note: You cannot rotate an envelope. |
| 16 | + |
| 17 | +### Example 1 |
| 18 | + |
| 19 | +``` |
| 20 | +Input: envelopes = [[5,4],[6,4],[6,7],[2,3]] |
| 21 | +Output: 3 |
| 22 | +Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). |
| 23 | +``` |
| 24 | + |
| 25 | +### Example 2 |
| 26 | + |
| 27 | +``` |
| 28 | +Input: envelopes = [[1,1],[1,1],[1,1]] |
| 29 | +Output: 1 |
| 30 | +``` |
| 31 | + |
| 32 | +### Constraints |
| 33 | + |
| 34 | +`1 <= envelopes.length <= 5000` |
| 35 | + |
| 36 | +`envelopes[i].length == 2` |
| 37 | + |
| 38 | +`1 <= wi, hi <= 104` |
| 39 | + |
| 40 | +<details> |
| 41 | + <summary>Solutions (Click to expand)</summary> |
| 42 | + |
| 43 | +### Explanation |
| 44 | + |
| 45 | +#### Solution |
| 46 | + |
| 47 | +##### Intuition |
| 48 | + |
| 49 | +Our greedy solution for this problem would be to sort the envelopes based on their width and height. That way our problem would be reduced to finding the longest increasing subsequence or in other words find the longest subsequence of the sorted envelopes where both the height and width increase. |
| 50 | + |
| 51 | +``` |
| 52 | +[[5,4],[6,4],[6,7],[2,3]] |
| 53 | +
|
| 54 | +sort... |
| 55 | +
|
| 56 | +[[2,3],[5,4],[6,4],[6,7]] |
| 57 | +``` |
| 58 | + |
| 59 | +In fact if the envelopes are sorted by width, then we only need to find the longest increasing subsequence of heights. |
| 60 | + |
| 61 | +In the case for envelopes that have the same widths by different heights, we would want to insert the envelopes with the largest height first. If there are other envelopes that have a smaller height and can fit into the subsequence then we can replace it afterwards. |
| 62 | + |
| 63 | +``` |
| 64 | +[[2,3],[5,4],[6,7],[6,4]] |
| 65 | +
|
| 66 | +[[2, 3], [5, 4], [6, 7]] |
| 67 | +``` |
| 68 | + |
| 69 | +##### Implementation |
| 70 | + |
| 71 | +We want to sort the envelopes by ascending widths and descending heights. That way we can find the longest increasing subsequence based on heights. |
| 72 | + |
| 73 | +We will use an array where we can insert out envelopes. We will use binary search to find the position in the array we can insert the envelope into. If the envelope gets inserted at the end of the array we can increase the count. |
| 74 | + |
| 75 | +``` |
| 76 | +[] |
| 77 | +
|
| 78 | +[[2,3],[5,4],[6,7],[6,4]] |
| 79 | + ^ |
| 80 | +[3] |
| 81 | +
|
| 82 | +[[2,3],[5,4],[6,7],[6,4]] |
| 83 | + ^ |
| 84 | +[3, 4] |
| 85 | +
|
| 86 | +[[2,3],[5,4],[6,7],[6,4]] |
| 87 | + ^ |
| 88 | +[3, 4, 7] |
| 89 | +
|
| 90 | +[[2,3],[5,4],[6,7],[6,4]] |
| 91 | + ^ |
| 92 | +[3, 4, 7] |
| 93 | +
|
| 94 | +``` |
| 95 | + |
| 96 | +Time: `O(N * log * N)` |
| 97 | + |
| 98 | +Space: `O(N)` |
| 99 | + |
| 100 | +- [JavaScript](./russian-doll-envelopes.js) |
| 101 | +- [TypeScript](./russian-doll-envelopes.ts) |
| 102 | +- [Java](./russian-doll-envelopes.java) |
| 103 | +- [Go](./russian-doll-envelopes.go) |
| 104 | + |
| 105 | +</details> |
0 commit comments