Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 354 #870

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions leetcode/301-400/0354.Russian-Doll-Envelopes/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# [354.Russian Doll Envelopes][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a 2D array of integers `envelopes` where `envelopes[i] = [wi, hi]` represents the width and the height of an envelope.

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.

Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).

**Note**: You cannot rotate an envelope.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Russian Doll Envelopes
```go
```

Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1
```

## 结语

Expand Down
36 changes: 34 additions & 2 deletions leetcode/301-400/0354.Russian-Doll-Envelopes/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(envelopes [][]int) int {
// 先根据宽度排序,然后根据高度排序
sort.Slice(envelopes, func(i, j int) bool {
if envelopes[i][0] == envelopes[j][0] {
//return envelopes[i][1] < envelopes[j][1]
// 2, 4
// 1 1
// 这情况,导致成两个,实际宽度相同,只能保留一个
return envelopes[i][1] > envelopes[j][1]

}
return envelopes[i][0] < envelopes[j][0]
})

// 2, 4, 1, 3, 5
// 1 1, 2, 2, 3
// 1, 4
// 这个问题就是,最长公共子序列问题了
lis := make([]int, 0)
for _, h := range envelopes {
index := sort.Search(len(lis), func(i int) bool {
return lis[i] >= h[1]
})
if index == len(lis) {
//找不到
lis = append(lis, h[1])
} else {
lis[index] = h[1]
}
}

return len(lis)
}
4 changes: 2 additions & 2 deletions leetcode/301-400/0354.Russian-Doll-Envelopes/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)

Check failure on line 24 in leetcode/301-400/0354.Russian-Doll-Envelopes/Solution_test.go

View workflow job for this annotation

GitHub Actions / Test Solution Cases

cannot use c.inputs (variable of type bool) as [][]int value in argument to Solution
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
Expand All @@ -30,10 +30,10 @@
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}