Skip to content

Commit 1ab2948

Browse files
authored
Merge pull request #1261 from 0xff-dev/3201
Add solution and test-cases for problem 3201
2 parents fb0304b + 8ab9820 commit 1ab2948

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/README.md

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
# [3201.Find the Maximum Length of Valid Subsequence I][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 an integer array `nums`.
5+
A `subsequence` `sub` of `nums` with length `x` is called **valid** if it satisfies:
6+
7+
- `(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2`.
8+
9+
Return the length of the **longest valid** subsequence of `nums`.
10+
11+
A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [1,2,3,4]
17+
18+
Output: 4
19+
20+
Explanation:
21+
22+
The longest valid subsequence is [1, 2, 3, 4].
23+
```
24+
25+
**Example 2:**
26+
1327
```
28+
Input: nums = [1,2,1,1,2,1,2]
29+
30+
Output: 6
31+
32+
Explanation:
1433
15-
## 题意
16-
> ...
34+
The longest valid subsequence is [1, 2, 1, 2, 1, 2].
35+
```
1736

18-
## 题解
37+
**Example 3:**
1938

20-
### 思路1
21-
> ...
22-
Find the Maximum Length of Valid Subsequence I
23-
```go
2439
```
40+
Input: nums = [1,3]
41+
42+
Output: 2
2543
44+
Explanation:
45+
46+
The longest valid subsequence is [1, 3].
47+
```
2648

2749
## 结语
2850

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
// 要么一连串偶数(奇数),可以保证%2都是0
5+
a, b := 0, 0
6+
for _, n := range nums {
7+
if n&1 == 0 {
8+
a++
9+
continue
10+
}
11+
b++
12+
}
13+
ans := max(a, b)
14+
// 开始找一个奇数一个偶数, 保证了结果一定是奇数
15+
evenLen, oddLen := 0, 0
16+
for _, n := range nums {
17+
if n&1 == 0 {
18+
evenLen = oddLen + 1
19+
continue
20+
}
21+
oddLen = evenLen + 1
22+
}
23+
return max(ans, oddLen, evenLen)
524
}

leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{1, 2, 3, 4}, 4},
17+
{"TestCase2", []int{1, 2, 1, 1, 2, 1, 2}, 6},
18+
{"TestCase3", []int{1, 3}, 2},
1919
}
2020

2121
// 开始测试
@@ -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)