Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c14840

Browse files
committed
Add solution and test-cases for problem 491
1 parent e8bf636 commit 0c14840

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

‎leetcode/401-500/0491.Increasing-Subsequences/README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
# [491.Increasing Subsequences][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+
Given an integer array `nums`, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in **any order**.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
Input: nums = [4,6,7,7]
10+
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
1411
15-
## 题意
16-
> ...
12+
```
1713

18-
## 题解
14+
**Example 2:**
1915

20-
### 思路1
21-
> ...
22-
Increasing Subsequences
23-
```go
2416
```
25-
17+
Input: nums = [4,4,3,2,1]
18+
Output: [[4,4]]
19+
```
2620

2721
## 结语
2822

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) [][]int {
4+
ans := make([][]int, 0)
5+
l := len(nums)
6+
visited := make(map[[15]int]struct{})
7+
var dfs func(int, [15]int, int, map[[15]int]struct{})
8+
dfs = func(start int, path [15]int, idx int, visited map[[15]int]struct{}) {
9+
if start >= l {
10+
return
11+
}
12+
for next := start + 1; next < l; next++ {
13+
if nums[next] < nums[start] {
14+
continue
15+
}
16+
path[idx] = nums[next]
17+
if _, ok := visited[path]; ok {
18+
continue
19+
}
20+
visited[path] = struct{}{}
21+
dst := make([]int, idx+1)
22+
copy(dst, path[:idx+1])
23+
ans = append(ans, dst)
24+
dfs(next, path, idx+1, visited)
25+
}
26+
}
27+
for i := 0; i < l-1; i++ {
28+
path := [15]int{nums[i], -101, -101, -101, -101, -101, -101, -101, -101, -101, -101, -101, -101, -101, -101}
29+
dfs(i, path, 1, visited)
30+
}
31+
return ans
532
}

‎leetcode/401-500/0491.Increasing-Subsequences/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 6, 7, 7}, [][]int{{4, 6}, {4, 6, 7}, {4, 6, 7, 7}, {4, 7}, {4, 7, 7}, {6, 7}, {6, 7, 7}, {7, 7}}},
17+
{"TestCase2", []int{4, 4, 3, 2, 1}, [][]int{{4, 4}}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
//压力测试
32+
//压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
//使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /