From 8ab9820a407af41059fbcc96a17d8fe0b7a063c7 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 16 Jul 2025 09:14:39 +0800 Subject: [PATCH] Add solution and test-cases for problem 3201 --- .../README.md | 46 ++++++++++++++----- .../Solution.go | 23 +++++++++- .../Solution_test.go | 14 +++--- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/README.md b/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/README.md index 5dd320804..068539db4 100755 --- a/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/README.md +++ b/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/README.md @@ -1,28 +1,50 @@ # [3201.Find the Maximum Length of Valid Subsequence I][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`. +A `subsequence` `sub` of `nums` with length `x` is called **valid** if it satisfies: + +- `(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2`. + +Return the length of the **longest valid** subsequence of `nums`. + +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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,3,4] + +Output: 4 + +Explanation: + +The longest valid subsequence is [1, 2, 3, 4]. +``` + +**Example 2:** + ``` +Input: nums = [1,2,1,1,2,1,2] + +Output: 6 + +Explanation: -## 题意 -> ... +The longest valid subsequence is [1, 2, 1, 2, 1, 2]. +``` -## 题解 +**Example 3:** -### 思路1 -> ... -Find the Maximum Length of Valid Subsequence I -```go ``` +Input: nums = [1,3] + +Output: 2 +Explanation: + +The longest valid subsequence is [1, 3]. +``` ## 结语 diff --git a/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution.go b/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution.go index d115ccf5e..652ff6956 100644 --- a/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution.go +++ b/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + // 要么一连串偶数(奇数),可以保证%2都是0 + a, b := 0, 0 + for _, n := range nums { + if n&1 == 0 { + a++ + continue + } + b++ + } + ans := max(a, b) + // 开始找一个奇数一个偶数, 保证了结果一定是奇数 + evenLen, oddLen := 0, 0 + for _, n := range nums { + if n&1 == 0 { + evenLen = oddLen + 1 + continue + } + oddLen = evenLen + 1 + } + return max(ans, oddLen, evenLen) } diff --git a/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution_test.go b/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution_test.go index 14ff50eb4..e57cfa564 100644 --- a/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution_test.go +++ b/leetcode/3201-3300/3201.Find-the-Maximum-Length-of-Valid-Subsequence-I/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3, 4}, 4}, + {"TestCase2", []int{1, 2, 1, 1, 2, 1, 2}, 6}, + {"TestCase3", []int{1, 3}, 2}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }