Skip to content

Commit ca04c47

Browse files
chore: Merge pull request #189 from saulmaldonado/feat/russian-doll-envelopes
russian doll envelopes
2 parents bcfa222 + 70235eb commit ca04c47

5 files changed

+279
-0
lines changed
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Russian Doll Envelopes
2+
3+
## Difficulty
4+
5+
![Hard](https://img.shields.io/badge/hard-d9534f?style=for-the-badge&logoColor=white)
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package russiandollenvelopes
2+
3+
import "sort"
4+
5+
func maxEnvelopes(envelopes [][]int) int {
6+
n := len(envelopes)
7+
8+
sort.Slice(envelopes, func(i, j int) bool {
9+
if envelopes[i][0] == envelopes[j][0] {
10+
return envelopes[j][1] < envelopes[i][1]
11+
}
12+
return envelopes[i][0] < envelopes[j][0]
13+
})
14+
15+
count := 0
16+
dp := make([]int, n)
17+
18+
for _, envelope := range envelopes {
19+
i := binarySearch(dp, 0, count, envelope[1])
20+
21+
dp[i] = envelope[1]
22+
23+
if i == count {
24+
count++
25+
}
26+
}
27+
return count
28+
}
29+
30+
func binarySearch(dp []int, start int, end int, envelope int) int {
31+
for start < end {
32+
mid := start + ((end - start) / 2)
33+
34+
if dp[mid] == envelope {
35+
return mid
36+
}
37+
38+
if dp[mid] < envelope {
39+
start = mid + 1
40+
} else {
41+
end = mid
42+
}
43+
}
44+
45+
return start
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public int maxEnvelopes(int[][] envelopes) {
5+
int n = envelopes.length;
6+
7+
Arrays.sort(envelopes, (a, b) -> {
8+
if (a[0] == b[0]) {
9+
return b[1] - a[1];
10+
}
11+
12+
return a[0] - b[0];
13+
});
14+
15+
int count = 0;
16+
int[] dp = new int[n];
17+
18+
for (int[] envelope : envelopes) {
19+
int i = Arrays.binarySearch(dp, 0, count, envelope[1]);
20+
21+
if (i < 0) {
22+
i = -(i + 1);
23+
}
24+
25+
dp[i] = envelope[1];
26+
27+
if (i == count) {
28+
count++;
29+
}
30+
}
31+
return count;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @param {number[][]} envelopes
3+
* @return {number}
4+
*/
5+
function maxEnvelopes(envelopes) {
6+
const n = envelopes.length;
7+
8+
envelopes.sort((a, b) => {
9+
if (a[0] === b[0]) {
10+
return b[1] - a[1];
11+
}
12+
13+
return a[0] - b[0];
14+
});
15+
16+
let count = 0;
17+
const dp = new Array(n);
18+
19+
for (const envelope of envelopes) {
20+
let i = binarySearch(dp, 0, count, envelope[1]);
21+
22+
dp[i] = envelope[1];
23+
24+
if (i === count) {
25+
count++;
26+
}
27+
}
28+
return count;
29+
}
30+
31+
/**
32+
*
33+
* @param {number[]} dp
34+
* @param {number} start
35+
* @param {number} end
36+
* @param {number} key
37+
*/
38+
function binarySearch(dp, start, end, key) {
39+
while (start < end) {
40+
let mid = start + Math.floor((end - start) / 2);
41+
42+
if (dp[mid] === key) {
43+
return mid;
44+
}
45+
46+
if (dp[mid] < key) {
47+
start = mid + 1;
48+
} else {
49+
end = mid;
50+
}
51+
}
52+
return start;
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function maxEnvelopes(envelopes: number[][]): number {
2+
const n = envelopes.length;
3+
4+
envelopes.sort((a, b) => {
5+
if (a[0] === b[0]) {
6+
return b[1] - a[1];
7+
}
8+
9+
return a[0] - b[0];
10+
});
11+
12+
let count = 0;
13+
const dp: number[] = new Array(n);
14+
15+
for (const envelope of envelopes) {
16+
let i = binarySearch(dp, 0, count, envelope[1]);
17+
18+
dp[i] = envelope[1];
19+
20+
if (i === count) {
21+
count++;
22+
}
23+
}
24+
return count;
25+
}
26+
27+
function binarySearch(dp: number[], start: number, end: number, key: number): number {
28+
while (start < end) {
29+
let mid = start + Math.floor((end - start) / 2);
30+
31+
if (dp[mid] === key) {
32+
return mid;
33+
}
34+
35+
if (dp[mid] < key) {
36+
start = mid + 1;
37+
} else {
38+
end = mid;
39+
}
40+
}
41+
return start;
42+
}

0 commit comments

Comments
 (0)