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

Add solution and test-cases for proble 3202 #1262

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

Merged
6boris merged 1 commit into 6boris:main from 0xff-dev:3202
Aug 3, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [3202.Find the Maximum Length of Valid Subsequence II][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 an integer array `nums` and a **positive** integer `k`.
A `subsequence` `sub` of `nums` with length `x` is called **valid** if it satisfies:

- `(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k`.

Return the length of the **longest valid** subsequence of `nums`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: nums = [1,2,3,4,5], k = 2

Output: 5

## 题意
> ...
Explanation:

## 题解
The longest valid subsequence is [1, 2, 3, 4, 5].
```

**Example 2:**

### 思路1
> ...
Find the Maximum Length of Valid Subsequence II
```go
```
Input: nums = [1,4,2,3,1,4], k = 3

Output: 4

Explanation:

The longest valid subsequence is [1, 4, 1, 4].
```

## 结语

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
package Solution

func Solution(x bool) bool {
return x
func longestAlternatingSubsequnece(l1, l2 []int) int {
a, b := 0, 0
ai, bi := 0, 0
for ai < len(l1) && bi < len(l2) {
if l1[ai] < l2[bi] {
ai++
a = b + 1
continue
}
b = a + 1
bi++
}
return max(a, b) + 1
}
func Solution(nums []int, k int) int {
group := make([][]int, k)
for i := range nums {
nums[i] %= k
group[nums[i]] = append(group[nums[i]], i)
}
// 1,0,1,0,1
ans, l := 0, len(nums)
sums := make(map[int]struct{})
for i := 0; i < l; i++ {
for j := i + 1; j < l; j++ {
sums[nums[i]+nums[j]] = struct{}{}
}
}

for sum := range sums {
used := map[int]struct{}{}
for i := 0; i < l; i++ {
if _, ok := used[nums[i]]; ok {
continue
}
used[nums[i]] = struct{}{}
target := (k + sum - nums[i]) % k
used[target] = struct{}{}

ll := len(group[target])
if ll == 0 {
continue
}
if nums[i] == target {
ans = max(ans, ll)
continue
}
ans = max(ans, longestAlternatingSubsequnece(group[nums[i]], group[target]))
}
}
return ans
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 4, 5}, 2, 5},
{"TestCase2", []int{1, 4, 2, 3, 1, 4}, 3, 4},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

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

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

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