Skip to content

feat: add solutions to lc problems: No.3201,3202 #3196

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
merged 1 commit into from
Jul 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,121 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:动态规划

我们令 $k = 2$。

根据题目描述,我们可以得知,对于子序列 $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$ 取模的结果相同。

我们可以使用动态规划的方法解决这个问题。定义状态 $f[x][y]$ 表示最后一项对 $k$ 取模为 $x$,倒数第二项对 $k$ 取模为 $y$ 的最长有效子序列的长度。初始时 $f[x][y] = 0$。

遍历数组 $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$。

答案为所有 $f[x][y]$ 中的最大值。

时间复杂度 $O(n \times k)$,空间复杂度 $O(k^2)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $k=2$。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def maximumLength(self, nums: List[int]) -> int:
k = 2
f = [[0] * k for _ in range(k)]
ans = 0
for x in nums:
x %= k
for j in range(k):
y = (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
return ans
```

#### Java

```java

class Solution {
public int maximumLength(int[] nums) {
int k = 2;
int[][] f = new int[k][k];
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int maximumLength(vector<int>& nums) {
int k = 2;
int f[k][k];
memset(f, 0, sizeof(f));
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = max(ans, f[x][y]);
}
}
return ans;
}
};
```

#### Go

```go
func maximumLength(nums []int) (ans int) {
k := 2
f := make([][]int, k)
for i := range f {
f[i] = make([]int, k)
}
for _, x := range nums {
x %= k
for j := 0; j < k; j++ {
y := (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
}
}
return
}
```

#### TypeScript

```ts
function maximumLength(nums: number[]): number {
const k = 2;
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
let ans: number = 0;
for (let x of nums) {
x %= k;
for (let j = 0; j < k; ++j) {
const y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,121 @@ You are given an integer array <code>nums</code>.

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We set $k = 2$.

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.

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$.

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$.

The answer is the maximum value among all $f[x][y]$.

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$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def maximumLength(self, nums: List[int]) -> int:
k = 2
f = [[0] * k for _ in range(k)]
ans = 0
for x in nums:
x %= k
for j in range(k):
y = (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
return ans
```

#### Java

```java

class Solution {
public int maximumLength(int[] nums) {
int k = 2;
int[][] f = new int[k][k];
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int maximumLength(vector<int>& nums) {
int k = 2;
int f[k][k];
memset(f, 0, sizeof(f));
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = max(ans, f[x][y]);
}
}
return ans;
}
};
```

#### Go

```go
func maximumLength(nums []int) (ans int) {
k := 2
f := make([][]int, k)
for i := range f {
f[i] = make([]int, k)
}
for _, x := range nums {
x %= k
for j := 0; j < k; j++ {
y := (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
}
}
return
}
```

#### TypeScript

```ts
function maximumLength(nums: number[]): number {
const k = 2;
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
let ans: number = 0;
for (let x of nums) {
x %= k;
for (let j = 0; j < k; ++j) {
const y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int maximumLength(vector<int>& nums) {
int k = 2;
int f[k][k];
memset(f, 0, sizeof(f));
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = max(ans, f[x][y]);
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func maximumLength(nums []int) (ans int) {
k := 2
f := make([][]int, k)
for i := range f {
f[i] = make([]int, k)
}
for _, x := range nums {
x %= k
for j := 0; j < k; j++ {
y := (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int maximumLength(int[] nums) {
int k = 2;
int[][] f = new int[k][k];
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def maximumLength(self, nums: List[int]) -> int:
k = 2
f = [[0] * k for _ in range(k)]
ans = 0
for x in nums:
x %= k
for j in range(k):
y = (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function maximumLength(nums: number[]): number {
const k = 2;
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
let ans: number = 0;
for (let x of nums) {
x %= k;
for (let j = 0; j < k; ++j) {
const y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
Loading
Loading