Skip to content

Commit f9acb7e

Browse files
committed
Add solution and test-cases for problem 354
1 parent 09a1dcb commit f9acb7e

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

leetcode/301-400/0354.Russian-Doll-Envelopes/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# [354.Russian Doll Envelopes][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D array of integers `envelopes` where `envelopes[i] = [wi, hi]` represents the width and the height of an envelope.
5+
6+
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.
7+
8+
Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).
9+
10+
**Note**: You cannot rotate an envelope.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
16+
Output: 3
17+
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Russian Doll Envelopes
23-
```go
2422
```
25-
23+
Input: envelopes = [[1,1],[1,1],[1,1]]
24+
Output: 1
25+
```
2626

2727
## 结语
2828

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(envelopes [][]int) int {
6+
// 先根据宽度排序,然后根据高度排序
7+
sort.Slice(envelopes, func(i, j int) bool {
8+
if envelopes[i][0] == envelopes[j][0] {
9+
//return envelopes[i][1] < envelopes[j][1]
10+
// 2, 4
11+
// 1 1
12+
// 这情况,导致成两个,实际宽度相同,只能保留一个
13+
return envelopes[i][1] > envelopes[j][1]
14+
15+
}
16+
return envelopes[i][0] < envelopes[j][0]
17+
})
18+
19+
// 2, 4, 1, 3, 5
20+
// 1 1, 2, 2, 3
21+
// 1, 4
22+
// 这个问题就是,最长公共子序列问题了
23+
lis := make([]int, 0)
24+
for _, h := range envelopes {
25+
index := sort.Search(len(lis), func(i int) bool {
26+
return lis[i] >= h[1]
27+
})
28+
if index == len(lis) {
29+
//找不到
30+
lis = append(lis, h[1])
31+
} else {
32+
lis[index] = h[1]
33+
}
34+
}
35+
36+
return len(lis)
537
}

leetcode/301-400/0354.Russian-Doll-Envelopes/Solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)