Skip to content

Commit be560c9

Browse files
authored
feat: add solutions to lc problems: No.3201,3202 (doocs#3196)
* No.3201.Find the Maximum Length of Valid Subsequence I * No.3202.Find the Maximum Length of Valid Subsequence II
1 parent 45c7437 commit be560c9

File tree

14 files changed

+504
-17
lines changed

14 files changed

+504
-17
lines changed

solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md

+93-4
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,121 @@ tags:
8282

8383
<!-- solution:start -->
8484

85-
### 方法一
85+
### 方法一:动态规划
86+
87+
我们令 $k = 2$。
88+
89+
根据题目描述,我们可以得知,对于子序列 $a_1, a_2, a_3, \cdots, a_x$,如果满足 $(a_1 + a_2) \bmod k = (a_2 + a_3) \bmod k$。那么 $a_1 \bmod k = a_3 \bmod k$。也即是说,所有奇数项元素对 $k$ 取模的结果相同,所有偶数项元素对 $k$ 取模的结果相同。
90+
91+
我们可以使用动态规划的方法解决这个问题。定义状态 $f[x][y]$ 表示最后一项对 $k$ 取模为 $x$,倒数第二项对 $k$ 取模为 $y$ 的最长有效子序列的长度。初始时 $f[x][y] = 0$。
92+
93+
遍历数组 $nums$,对于每一个数 $x$,我们得到 $x = x \bmod k$。然后我们可以枚举序列连续两个数对 $j$ 取模结果相同,其中 $j \in [0, k)$。那么 $x$ 的前一个数对 $k$ 取模结果为 $y = (j - x + k) \bmod k$。此时 $f[x][y] = f[y][x] + 1$。
94+
95+
答案为所有 $f[x][y]$ 中的最大值。
96+
97+
时间复杂度 $O(n \times k)$,空间复杂度 $O(k^2)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $k=2$。
8698

8799
<!-- tabs:start -->
88100

89101
#### Python3
90102

91103
```python
92-
104+
class Solution:
105+
def maximumLength(self, nums: List[int]) -> int:
106+
k = 2
107+
f = [[0] * k for _ in range(k)]
108+
ans = 0
109+
for x in nums:
110+
x %= k
111+
for j in range(k):
112+
y = (j - x + k) % k
113+
f[x][y] = f[y][x] + 1
114+
ans = max(ans, f[x][y])
115+
return ans
93116
```
94117

95118
#### Java
96119

97120
```java
98-
121+
class Solution {
122+
public int maximumLength(int[] nums) {
123+
int k = 2;
124+
int[][] f = new int[k][k];
125+
int ans = 0;
126+
for (int x : nums) {
127+
x %= k;
128+
for (int j = 0; j < k; ++j) {
129+
int y = (j - x + k) % k;
130+
f[x][y] = f[y][x] + 1;
131+
ans = Math.max(ans, f[x][y]);
132+
}
133+
}
134+
return ans;
135+
}
136+
}
99137
```
100138

101139
#### C++
102140

103141
```cpp
104-
142+
class Solution {
143+
public:
144+
int maximumLength(vector<int>& nums) {
145+
int k = 2;
146+
int f[k][k];
147+
memset(f, 0, sizeof(f));
148+
int ans = 0;
149+
for (int x : nums) {
150+
x %= k;
151+
for (int j = 0; j < k; ++j) {
152+
int y = (j - x + k) % k;
153+
f[x][y] = f[y][x] + 1;
154+
ans = max(ans, f[x][y]);
155+
}
156+
}
157+
return ans;
158+
}
159+
};
105160
```
106161
107162
#### Go
108163
109164
```go
165+
func maximumLength(nums []int) (ans int) {
166+
k := 2
167+
f := make([][]int, k)
168+
for i := range f {
169+
f[i] = make([]int, k)
170+
}
171+
for _, x := range nums {
172+
x %= k
173+
for j := 0; j < k; j++ {
174+
y := (j - x + k) % k
175+
f[x][y] = f[y][x] + 1
176+
ans = max(ans, f[x][y])
177+
}
178+
}
179+
return
180+
}
181+
```
110182

183+
#### TypeScript
184+
185+
```ts
186+
function maximumLength(nums: number[]): number {
187+
const k = 2;
188+
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
189+
let ans: number = 0;
190+
for (let x of nums) {
191+
x %= k;
192+
for (let j = 0; j < k; ++j) {
193+
const y = (j - x + k) % k;
194+
f[x][y] = f[y][x] + 1;
195+
ans = Math.max(ans, f[x][y]);
196+
}
197+
}
198+
return ans;
199+
}
111200
```
112201

113202
<!-- tabs:end -->

solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md

+93-4
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,121 @@ You are given an integer array <code>nums</code>.
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: Dynamic Programming
84+
85+
We set $k = 2$.
86+
87+
Based on the problem description, we know that for a subsequence $a_1, a_2, a_3, \cdots, a_x$, if it satisfies $(a_1 + a_2) \bmod k = (a_2 + a_3) \bmod k$. Then $a_1 \bmod k = a_3 \bmod k$. This means that the result of taking modulo $k$ for all odd-indexed elements is the same, and the result for all even-indexed elements is the same as well.
88+
89+
We can solve this problem using dynamic programming. Define the state $f[x][y]$ as the length of the longest valid subsequence where the last element modulo $k$ equals $x$, and the second to last element modulo $k$ equals $y$. Initially, $f[x][y] = 0$.
90+
91+
Iterate through the array $nums$, and for each number $x$, we get $x = x \bmod k$. Then, we can enumerate the sequences where two consecutive numbers modulo $j$ yield the same result, where $j \in [0, k)$. Thus, the previous number modulo $k$ would be $y = (j - x + k) \bmod k$. At this point, $f[x][y] = f[y][x] + 1$.
92+
93+
The answer is the maximum value among all $f[x][y]$.
94+
95+
The time complexity is $O(n \times k)$, and the space complexity is $O(k^2)$. Here, $n$ is the length of the array $\text{nums}$, and $k=2$.
8496

8597
<!-- tabs:start -->
8698

8799
#### Python3
88100

89101
```python
90-
102+
class Solution:
103+
def maximumLength(self, nums: List[int]) -> int:
104+
k = 2
105+
f = [[0] * k for _ in range(k)]
106+
ans = 0
107+
for x in nums:
108+
x %= k
109+
for j in range(k):
110+
y = (j - x + k) % k
111+
f[x][y] = f[y][x] + 1
112+
ans = max(ans, f[x][y])
113+
return ans
91114
```
92115

93116
#### Java
94117

95118
```java
96-
119+
class Solution {
120+
public int maximumLength(int[] nums) {
121+
int k = 2;
122+
int[][] f = new int[k][k];
123+
int ans = 0;
124+
for (int x : nums) {
125+
x %= k;
126+
for (int j = 0; j < k; ++j) {
127+
int y = (j - x + k) % k;
128+
f[x][y] = f[y][x] + 1;
129+
ans = Math.max(ans, f[x][y]);
130+
}
131+
}
132+
return ans;
133+
}
134+
}
97135
```
98136

99137
#### C++
100138

101139
```cpp
102-
140+
class Solution {
141+
public:
142+
int maximumLength(vector<int>& nums) {
143+
int k = 2;
144+
int f[k][k];
145+
memset(f, 0, sizeof(f));
146+
int ans = 0;
147+
for (int x : nums) {
148+
x %= k;
149+
for (int j = 0; j < k; ++j) {
150+
int y = (j - x + k) % k;
151+
f[x][y] = f[y][x] + 1;
152+
ans = max(ans, f[x][y]);
153+
}
154+
}
155+
return ans;
156+
}
157+
};
103158
```
104159
105160
#### Go
106161
107162
```go
163+
func maximumLength(nums []int) (ans int) {
164+
k := 2
165+
f := make([][]int, k)
166+
for i := range f {
167+
f[i] = make([]int, k)
168+
}
169+
for _, x := range nums {
170+
x %= k
171+
for j := 0; j < k; j++ {
172+
y := (j - x + k) % k
173+
f[x][y] = f[y][x] + 1
174+
ans = max(ans, f[x][y])
175+
}
176+
}
177+
return
178+
}
179+
```
108180

181+
#### TypeScript
182+
183+
```ts
184+
function maximumLength(nums: number[]): number {
185+
const k = 2;
186+
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
187+
let ans: number = 0;
188+
for (let x of nums) {
189+
x %= k;
190+
for (let j = 0; j < k; ++j) {
191+
const y = (j - x + k) % k;
192+
f[x][y] = f[y][x] + 1;
193+
ans = Math.max(ans, f[x][y]);
194+
}
195+
}
196+
return ans;
197+
}
109198
```
110199

111200
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maximumLength(vector<int>& nums) {
4+
int k = 2;
5+
int f[k][k];
6+
memset(f, 0, sizeof(f));
7+
int ans = 0;
8+
for (int x : nums) {
9+
x %= k;
10+
for (int j = 0; j < k; ++j) {
11+
int y = (j - x + k) % k;
12+
f[x][y] = f[y][x] + 1;
13+
ans = max(ans, f[x][y]);
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func maximumLength(nums []int) (ans int) {
2+
k := 2
3+
f := make([][]int, k)
4+
for i := range f {
5+
f[i] = make([]int, k)
6+
}
7+
for _, x := range nums {
8+
x %= k
9+
for j := 0; j < k; j++ {
10+
y := (j - x + k) % k
11+
f[x][y] = f[y][x] + 1
12+
ans = max(ans, f[x][y])
13+
}
14+
}
15+
return
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maximumLength(int[] nums) {
3+
int k = 2;
4+
int[][] f = new int[k][k];
5+
int ans = 0;
6+
for (int x : nums) {
7+
x %= k;
8+
for (int j = 0; j < k; ++j) {
9+
int y = (j - x + k) % k;
10+
f[x][y] = f[y][x] + 1;
11+
ans = Math.max(ans, f[x][y]);
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maximumLength(self, nums: List[int]) -> int:
3+
k = 2
4+
f = [[0] * k for _ in range(k)]
5+
ans = 0
6+
for x in nums:
7+
x %= k
8+
for j in range(k):
9+
y = (j - x + k) % k
10+
f[x][y] = f[y][x] + 1
11+
ans = max(ans, f[x][y])
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maximumLength(nums: number[]): number {
2+
const k = 2;
3+
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
4+
let ans: number = 0;
5+
for (let x of nums) {
6+
x %= k;
7+
for (let j = 0; j < k; ++j) {
8+
const y = (j - x + k) % k;
9+
f[x][y] = f[y][x] + 1;
10+
ans = Math.max(ans, f[x][y]);
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)