diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md index a98471f360059..1cadd23dd13e2 100644 --- a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md @@ -82,32 +82,121 @@ tags: -### 方法一 +### 方法一:动态规划 + +我们令 $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$。 #### 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& 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; +} ``` diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md index 39dfc41a73f54..64aa506195bdf 100644 --- a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md @@ -80,32 +80,121 @@ You are given an integer array nums. -### 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$. #### 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& 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; +} ``` diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.cpp b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.cpp new file mode 100644 index 0000000000000..10e905e27b459 --- /dev/null +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maximumLength(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.go b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.go new file mode 100644 index 0000000000000..67b60ad3dcf4c --- /dev/null +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.java b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.java new file mode 100644 index 0000000000000..6afdfb49d2b99 --- /dev/null +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.py b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.py new file mode 100644 index 0000000000000..7a7b3cbf5bac6 --- /dev/null +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.py @@ -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 diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.ts b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.ts new file mode 100644 index 0000000000000..f678540fa7cef --- /dev/null +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/Solution.ts @@ -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; +} diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md index 54b682ad38c54..6b6d8c5488a09 100644 --- a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md @@ -68,32 +68,112 @@ tags: -### 方法一 +### 方法一:动态规划 - +根据题目描述,我们可以得知,对于子序列 $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$ 为给定的正整数。 #### Python3 ```python - +class Solution: + def maximumLength(self, nums: List[int], k: int) -> int: + 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) { + 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& nums, int k) { + 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, k int) (ans int) { + 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[], k: number): number { + 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; +} ``` diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md index fb4b41acfbcf5..74cad2e6c6e8c 100644 --- a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md @@ -65,32 +65,114 @@ Return the length of the longest valid subsequ -### Solution 1 +### Solution 1: Dynamic Programming + +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$ is the given positive integer. #### Python3 ```python - +class Solution: + def maximumLength(self, nums: List[int], k: int) -> int: + 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) { + 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& nums, int k) { + 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, k int) (ans int) { + 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[], k: number): number { + 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; +} ``` diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.cpp b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.cpp new file mode 100644 index 0000000000000..4966fac5fc8ce --- /dev/null +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maximumLength(vector& nums, int k) { + 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; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.go b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.go new file mode 100644 index 0000000000000..8a95df89d4e5d --- /dev/null +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.go @@ -0,0 +1,15 @@ +func maximumLength(nums []int, k int) (ans int) { + 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 +} \ No newline at end of file diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.java b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.java new file mode 100644 index 0000000000000..2838abac7d5f0 --- /dev/null +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int maximumLength(int[] nums, int k) { + 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; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.py b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.py new file mode 100644 index 0000000000000..6b5cdffe5bbca --- /dev/null +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def maximumLength(self, nums: List[int], k: int) -> int: + 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 diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.ts b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.ts new file mode 100644 index 0000000000000..8947f504d8f65 --- /dev/null +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/Solution.ts @@ -0,0 +1,13 @@ +function maximumLength(nums: number[], k: number): number { + 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; +}