Skip to content

Commit fb0304b

Browse files
authored
Merge pull request #1262 from 0xff-dev/3202
Add solution and test-cases for proble 3202
2 parents a1721c9 + a9c4c73 commit fb0304b

File tree

3 files changed

+82
-25
lines changed

3 files changed

+82
-25
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [3202.Find the Maximum Length of Valid Subsequence II][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` and a **positive** integer `k`.
5+
A `subsequence` `sub` of `nums` with length `x` is called **valid** if it satisfies:
6+
7+
- `(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k`.
8+
9+
Return the length of the **longest valid** subsequence of `nums`.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
14+
Input: nums = [1,2,3,4,5], k = 2
15+
16+
Output: 5
1417
15-
## 题意
16-
> ...
18+
Explanation:
1719
18-
## 题解
20+
The longest valid subsequence is [1, 2, 3, 4, 5].
21+
```
22+
23+
**Example 2:**
1924

20-
### 思路1
21-
> ...
22-
Find the Maximum Length of Valid Subsequence II
23-
```go
2425
```
26+
Input: nums = [1,4,2,3,1,4], k = 3
27+
28+
Output: 4
2529
30+
Explanation:
31+
32+
The longest valid subsequence is [1, 4, 1, 4].
33+
```
2634

2735
## 结语
2836

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

3-
func Solution(x bool) bool {
4-
return x
3+
func longestAlternatingSubsequnece(l1, l2 []int) int {
4+
a, b := 0, 0
5+
ai, bi := 0, 0
6+
for ai < len(l1) && bi < len(l2) {
7+
if l1[ai] < l2[bi] {
8+
ai++
9+
a = b + 1
10+
continue
11+
}
12+
b = a + 1
13+
bi++
14+
}
15+
return max(a, b) + 1
16+
}
17+
func Solution(nums []int, k int) int {
18+
group := make([][]int, k)
19+
for i := range nums {
20+
nums[i] %= k
21+
group[nums[i]] = append(group[nums[i]], i)
22+
}
23+
// 1,0,1,0,1
24+
ans, l := 0, len(nums)
25+
sums := make(map[int]struct{})
26+
for i := 0; i < l; i++ {
27+
for j := i + 1; j < l; j++ {
28+
sums[nums[i]+nums[j]] = struct{}{}
29+
}
30+
}
31+
32+
for sum := range sums {
33+
used := map[int]struct{}{}
34+
for i := 0; i < l; i++ {
35+
if _, ok := used[nums[i]]; ok {
36+
continue
37+
}
38+
used[nums[i]] = struct{}{}
39+
target := (k + sum - nums[i]) % k
40+
used[target] = struct{}{}
41+
42+
ll := len(group[target])
43+
if ll == 0 {
44+
continue
45+
}
46+
if nums[i] == target {
47+
ans = max(ans, ll)
48+
continue
49+
}
50+
ans = max(ans, longestAlternatingSubsequnece(group[nums[i]], group[target]))
51+
}
52+
}
53+
return ans
554
}

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

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)