Skip to content

Commit 82fe809

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0354
1 parent 296f2db commit 82fe809

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
- [打家劫舍](/solution/0100-0199/0198.House%20Robber/README.md)
136136
- [打家劫舍 II](/solution/0200-0299/0213.House%20Robber%20II/README.md)
137137
- [最长上升子序列](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
138+
- [俄罗斯套娃信封问题](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
138139

139140
### 混合问题
140141

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
124124
- [House Robber](/solution/0100-0199/0198.House%20Robber/README_EN.md)
125125
- [House Robber II](/solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
126126
- [Longest Increasing Subsequence](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
127+
- [Russian Doll Envelopes](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
127128

128129
### Misc
129130

solution/0300-0399/0354.Russian Doll Envelopes/README.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,60 @@
2323

2424
<!-- 这里可写通用的实现逻辑 -->
2525

26+
排序 + [最长递增子序列](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
27+
28+
按 w 进行升序排序,若 w 相同则按 h 降序排序。然后问题转换为求 h 数组的最长递增子序列长度。
29+
2630
<!-- tabs:start -->
2731

2832
### **Python3**
2933

3034
<!-- 这里可写当前语言的特殊实现逻辑 -->
3135

3236
```python
33-
37+
class Solution:
38+
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
39+
if not envelopes:
40+
return 0
41+
envelopes.sort(key=lambda x: (x[0], -x[1]))
42+
nums = [x[1] for x in envelopes]
43+
n = len(nums)
44+
dp = [1] * n
45+
res = 1
46+
for i in range(1, n):
47+
for j in range(i):
48+
if nums[j] < nums[i]:
49+
dp[i] = max(dp[i], dp[j] + 1)
50+
res = max(res, dp[i])
51+
return res
3452
```
3553

3654
### **Java**
3755

3856
<!-- 这里可写当前语言的特殊实现逻辑 -->
3957

4058
```java
41-
59+
class Solution {
60+
public int maxEnvelopes(int[][] envelopes) {
61+
int n;
62+
if (envelopes == null || (n = envelopes.length) == 0) return 0;
63+
Arrays.sort(envelopes, (a, b) -> {
64+
return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0];
65+
});
66+
int[] dp = new int[n];
67+
Arrays.fill(dp, 1);
68+
int res = 1;
69+
for (int i = 1; i < n; ++i) {
70+
for (int j = 0; j < i; ++j) {
71+
if (envelopes[j][1] < envelopes[i][1]) {
72+
dp[i] = Math.max(dp[i], dp[j] + 1);
73+
}
74+
}
75+
res = Math.max(res, dp[i]);
76+
}
77+
return res;
78+
}
79+
}
4280
```
4381

4482
### **...**

solution/0300-0399/0354.Russian Doll Envelopes/README_EN.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,47 @@ Rotation is not allowed.</p>
3535
### **Python3**
3636

3737
```python
38-
38+
class Solution:
39+
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
40+
if not envelopes:
41+
return 0
42+
envelopes.sort(key=lambda x: (x[0], -x[1]))
43+
nums = [x[1] for x in envelopes]
44+
n = len(nums)
45+
dp = [1] * n
46+
res = 1
47+
for i in range(1, n):
48+
for j in range(i):
49+
if nums[j] < nums[i]:
50+
dp[i] = max(dp[i], dp[j] + 1)
51+
res = max(res, dp[i])
52+
return res
3953
```
4054

4155
### **Java**
4256

4357
```java
44-
58+
class Solution {
59+
public int maxEnvelopes(int[][] envelopes) {
60+
int n;
61+
if (envelopes == null || (n = envelopes.length) == 0) return 0;
62+
Arrays.sort(envelopes, (a, b) -> {
63+
return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0];
64+
});
65+
int[] dp = new int[n];
66+
Arrays.fill(dp, 1);
67+
int res = 1;
68+
for (int i = 1; i < n; ++i) {
69+
for (int j = 0; j < i; ++j) {
70+
if (envelopes[j][1] < envelopes[i][1]) {
71+
dp[i] = Math.max(dp[i], dp[j] + 1);
72+
}
73+
}
74+
res = Math.max(res, dp[i]);
75+
}
76+
return res;
77+
}
78+
}
4579
```
4680

4781
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int maxEnvelopes(int[][] envelopes) {
3+
int n;
4+
if (envelopes == null || (n = envelopes.length) == 0) return 0;
5+
Arrays.sort(envelopes, (a, b) -> {
6+
return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0];
7+
});
8+
int[] dp = new int[n];
9+
Arrays.fill(dp, 1);
10+
int res = 1;
11+
for (int i = 1; i < n; ++i) {
12+
for (int j = 0; j < i; ++j) {
13+
if (envelopes[j][1] < envelopes[i][1]) {
14+
dp[i] = Math.max(dp[i], dp[j] + 1);
15+
}
16+
}
17+
res = Math.max(res, dp[i]);
18+
}
19+
return res;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
3+
if not envelopes:
4+
return 0
5+
envelopes.sort(key=lambda x: (x[0], -x[1]))
6+
nums = [x[1] for x in envelopes]
7+
n = len(nums)
8+
dp = [1] * n
9+
res = 1
10+
for i in range(1, n):
11+
for j in range(i):
12+
if nums[j] < nums[i]:
13+
dp[i] = max(dp[i], dp[j] + 1)
14+
res = max(res, dp[i])
15+
return res

0 commit comments

Comments
 (0)