You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md
+93-4
Original file line number
Diff line number
Diff line change
@@ -80,32 +80,121 @@ You are given an integer array <code>nums</code>.
80
80
81
81
<!-- solution:start -->
82
82
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$.
84
96
85
97
<!-- tabs:start -->
86
98
87
99
#### Python3
88
100
89
101
```python
90
-
102
+
classSolution:
103
+
defmaximumLength(self, nums: List[int]) -> int:
104
+
k =2
105
+
f = [[0] * k for _ inrange(k)]
106
+
ans =0
107
+
for x in nums:
108
+
x %= k
109
+
for j inrange(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
91
114
```
92
115
93
116
#### Java
94
117
95
118
```java
96
-
119
+
classSolution {
120
+
publicintmaximumLength(int[] nums) {
121
+
int k =2;
122
+
int[][] f =newint[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
+
}
97
135
```
98
136
99
137
#### C++
100
138
101
139
```cpp
102
-
140
+
classSolution {
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
+
};
103
158
```
104
159
105
160
#### Go
106
161
107
162
```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
+
```
108
180
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));
0 commit comments