(2));
+ for (int j = 0; j <= k; ++j) {
+ g[j][0] = (1LL * cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0)) % mod) % mod;
+ g[j][1] = (1LL * cnt1 * (f[j][0] + f[j][1]) % mod) % mod;
+ }
+ f = g;
+ }
+ return (f[k][0] + f[k][1]) % mod;
+ }
+};
+```
+
+#### Go
+
+```go
+func countOfArrays(n int, m int, k int) int {
+ const mod = 1e9 + 7
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ f := make([][2]int, k+1)
+ f[0][1] = 1
+
+ for i := 0; i < n; i++ {
+ g := make([][2]int, k+1)
+ for j := 0; j <= k; j++ {
+ g[j][0] = (cnt0 * (f[j][1] + func() int {
+ if j > 0 {
+ return f[j-1][0]
+ }
+ return 0
+ }()) % mod) % mod
+ g[j][1] = (cnt1 * (f[j][0] + f[j][1]) % mod) % mod
+ }
+ f = g
+ }
+
+ return (f[k][0] + f[k][1]) % mod
+}
+```
+
+#### TypeScript
+
+```ts
+function countOfArrays(n: number, m: number, k: number): number {
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ const f: number[][] = Array.from({ length: k + 1 }, () => [0, 0]);
+ f[0][1] = 1;
+ for (let i = 0; i < n; i++) {
+ const g: number[][] = Array.from({ length: k + 1 }, () => [0, 0]);
+ for (let j = 0; j <= k; j++) {
+ g[j][0] = ((cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0))) % mod) % mod;
+ g[j][1] = ((cnt1 * (f[j][0] + f[j][1])) % mod) % mod;
+ }
+ f.splice(0, f.length, ...g);
+ }
+ return (f[k][0] + f[k][1]) % mod;
+}
+```
+
+
+
+
+
+
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/README_EN.md b/solution/3300-3399/3339.Find the Number of K-Even Arrays/README_EN.md
new file mode 100644
index 0000000000000..572c259602450
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/README_EN.md
@@ -0,0 +1,527 @@
+---
+comments: true
+difficulty: Medium
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3339.Find%20the%20Number%20of%20K-Even%20Arrays/README_EN.md
+tags:
+ - Dynamic Programming
+---
+
+
+
+# [3339. Find the Number of K-Even Arrays 🔒](https://leetcode.com/problems/find-the-number-of-k-even-arrays)
+
+[中文文档](/solution/3300-3399/3339.Find%20the%20Number%20of%20K-Even%20Arrays/README.md)
+
+## Description
+
+
+
+You are given three integers n
, m
, and k
.
+
+An array arr
is called k-even if there are exactly k
indices such that, for each of these indices i
(0 <= i < n - 1
):
+
+
+ (arr[i] * arr[i + 1]) - arr[i] - arr[i + 1]
is even.
+
+
+Return the number of possible k-even arrays of size n
where all elements are in the range [1, m]
.
+
+Since the answer may be very large, return it modulo 109 + 7
.
+
+
+Example 1:
+
+
+
Input: n = 3, m = 4, k = 2
+
+
Output: 8
+
+
Explanation:
+
+
The 8 possible 2-even arrays are:
+
+
+ [2, 2, 2]
+ [2, 2, 4]
+ [2, 4, 2]
+ [2, 4, 4]
+ [4, 2, 2]
+ [4, 2, 4]
+ [4, 4, 2]
+ [4, 4, 4]
+
+
+
+Example 2:
+
+
+
Input: n = 5, m = 1, k = 0
+
+
Output: 1
+
+
Explanation:
+
+
The only 0-even array is [1, 1, 1, 1, 1]
.
+
+
+Example 3:
+
+
+
Input: n = 7, m = 7, k = 5
+
+
Output: 5832
+
+
+
+Constraints:
+
+
+ 1 <= n <= 750
+ 0 <= k <= n - 1
+ 1 <= m <= 1000
+
+
+
+
+## Solutions
+
+
+
+### Solution 1: Memoization Search
+
+Given the numbers $[1, m]$, there are $\textit{cnt0} = \lfloor \frac{m}{2} \rfloor$ even numbers and $\textit{cnt1} = m - \textit{cnt0}$ odd numbers.
+
+We design a function $\textit{dfs}(i, j, k)$, which represents the number of ways to fill up to the $i$-th position, with $j$ remaining positions needing to satisfy the condition, and the parity of the last position being $k$, where $k = 0$ indicates the last position is even, and $k = 1$ indicates the last position is odd. The answer is $\textit{dfs}(0, k, 1)$.
+
+The execution logic of the function $\textit{dfs}(i, j, k)$ is as follows:
+
+- If $j < 0$, it means the remaining positions are less than $0$, so return $0$;
+- If $i \ge n$, it means all positions are filled. If $j = 0$, it means the condition is satisfied, so return $1$, otherwise return $0$;
+- Otherwise, we can choose to fill with an odd or even number, calculate the number of ways for both, and return their sum.
+
+The time complexity is $O(n \times k)$, and the space complexity is $O(n \times k)$. Here, $n$ and $k$ are the parameters given in the problem.
+
+
+
+#### Python3
+
+```python
+class Solution:
+ def countOfArrays(self, n: int, m: int, k: int) -> int:
+ @cache
+ def dfs(i: int, j: int, k: int) -> int:
+ if j < 0:
+ return 0
+ if i >= n:
+ return int(j == 0)
+ return (
+ cnt1 * dfs(i + 1, j, 1) + cnt0 * dfs(i + 1, j - (k & 1 ^ 1), 0)
+ ) % mod
+
+ cnt0 = m // 2
+ cnt1 = m - cnt0
+ mod = 10**9 + 7
+ ans = dfs(0, k, 1)
+ dfs.cache_clear()
+ return ans
+```
+
+#### Java
+
+```java
+class Solution {
+ private Integer[][][] f;
+ private long cnt0, cnt1;
+ private final int mod = (int) 1e9 + 7;
+
+ public int countOfArrays(int n, int m, int k) {
+ f = new Integer[n][k + 1][2];
+ cnt0 = m / 2;
+ cnt1 = m - cnt0;
+ return dfs(0, k, 1);
+ }
+
+ private int dfs(int i, int j, int k) {
+ if (j < 0) {
+ return 0;
+ }
+ if (i >= f.length) {
+ return j == 0 ? 1 : 0;
+ }
+ if (f[i][j][k] != null) {
+ return f[i][j][k];
+ }
+ int a = (int) (cnt1 * dfs(i + 1, j, 1) % mod);
+ int b = (int) (cnt0 * dfs(i + 1, j - (k & 1 ^ 1), 0) % mod);
+ return f[i][j][k] = (a + b) % mod;
+ }
+}
+```
+
+#### C++
+
+```cpp
+class Solution {
+public:
+ int countOfArrays(int n, int m, int k) {
+ int f[n][k + 1][2];
+ memset(f, -1, sizeof(f));
+ const int mod = 1e9 + 7;
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ auto dfs = [&](auto&& dfs, int i, int j, int k) -> int {
+ if (j < 0) {
+ return 0;
+ }
+ if (i >= n) {
+ return j == 0 ? 1 : 0;
+ }
+ if (f[i][j][k] != -1) {
+ return f[i][j][k];
+ }
+ int a = 1LL * cnt1 * dfs(dfs, i + 1, j, 1) % mod;
+ int b = 1LL * cnt0 * dfs(dfs, i + 1, j - (k & 1 ^ 1), 0) % mod;
+ return f[i][j][k] = (a + b) % mod;
+ };
+ return dfs(dfs, 0, k, 1);
+ }
+};
+```
+
+#### Go
+
+```go
+func countOfArrays(n int, m int, k int) int {
+ f := make([][][2]int, n)
+ for i := range f {
+ f[i] = make([][2]int, k+1)
+ for j := range f[i] {
+ f[i][j] = [2]int{-1, -1}
+ }
+ }
+ const mod int = 1e9 + 7
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ var dfs func(int, int, int) int
+ dfs = func(i, j, k int) int {
+ if j < 0 {
+ return 0
+ }
+ if i >= n {
+ if j == 0 {
+ return 1
+ }
+ return 0
+ }
+ if f[i][j][k] != -1 {
+ return f[i][j][k]
+ }
+ a := cnt1 * dfs(i+1, j, 1) % mod
+ b := cnt0 * dfs(i+1, j-(k&1^1), 0) % mod
+ f[i][j][k] = (a + b) % mod
+ return f[i][j][k]
+ }
+ return dfs(0, k, 1)
+}
+```
+
+#### TypeScript
+
+```ts
+function countOfArrays(n: number, m: number, k: number): number {
+ const f = Array.from({ length: n }, () =>
+ Array.from({ length: k + 1 }, () => Array(2).fill(-1)),
+ );
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ const dfs = (i: number, j: number, k: number): number => {
+ if (j < 0) {
+ return 0;
+ }
+ if (i >= n) {
+ return j === 0 ? 1 : 0;
+ }
+ if (f[i][j][k] !== -1) {
+ return f[i][j][k];
+ }
+ const a = (cnt1 * dfs(i + 1, j, 1)) % mod;
+ const b = (cnt0 * dfs(i + 1, j - ((k & 1) ^ 1), 0)) % mod;
+ return (f[i][j][k] = (a + b) % mod);
+ };
+ return dfs(0, k, 1);
+}
+```
+
+
+
+
+
+
+
+### Solution 2: Dynamic Programming
+
+We can convert the memoized search from Solution 1 into dynamic programming.
+
+Define $f[i][j][k]$ to represent the number of ways to fill the $i$-th position, with $j$ positions satisfying the condition, and the parity of the previous position being $k$. The answer will be $\sum_{k = 0}^{1} f[n][k]$.
+
+Initially, we set $f[0][0][1] = 1$, indicating that after filling the $0$-th position, there are $0$ positions satisfying the condition, and the parity of the previous position is odd. All other $f[i][j][k]$ are initialized to $0$.
+
+The state transition equations are as follows:
+
+$$
+\begin{aligned}
+f[i][j][0] &= \left( f[i - 1][j][1] + \left( f[i - 1][j - 1][0] \text{ if } j > 0 \right) \right) \times \textit{cnt0} \bmod \textit{mod}, \\
+f[i][j][1] &= \left( f[i - 1][j][0] + f[i - 1][j][1] \right) \times \textit{cnt1} \bmod \textit{mod}.
+\end{aligned}
+$$
+
+The time complexity is $O(n \times k)$, and the space complexity is $O(n \times k)$, where $n$ and $k$ are the parameters given in the problem.
+
+
+
+#### Python3
+
+```python
+class Solution:
+ def countOfArrays(self, n: int, m: int, k: int) -> int:
+ f = [[[0] * 2 for _ in range(k + 1)] for _ in range(n + 1)]
+ cnt0 = m // 2
+ cnt1 = m - cnt0
+ mod = 10**9 + 7
+ f[0][0][1] = 1
+ for i in range(1, n + 1):
+ for j in range(k + 1):
+ f[i][j][0] = (
+ (f[i - 1][j][1] + (f[i - 1][j - 1][0] if j else 0)) * cnt0 % mod
+ )
+ f[i][j][1] = (f[i - 1][j][0] + f[i - 1][j][1]) * cnt1 % mod
+ return sum(f[n][k]) % mod
+```
+
+#### Java
+
+```java
+class Solution {
+ public int countOfArrays(int n, int m, int k) {
+ int[][][] f = new int[n + 1][k + 1][2];
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ final int mod = (int) 1e9 + 7;
+ f[0][0][1] = 1;
+ for (int i = 1; i <= n; ++i) {
+ for (int j = 0; j <= k; ++j) {
+ f[i][j][0]
+ = (int) (1L * cnt0 * (f[i - 1][j][1] + (j > 0 ? f[i - 1][j - 1][0] : 0)) % mod);
+ f[i][j][1] = (int) (1L * cnt1 * (f[i - 1][j][0] + f[i - 1][j][1]) % mod);
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod;
+ }
+}
+```
+
+#### C++
+
+```cpp
+class Solution {
+public:
+ int countOfArrays(int n, int m, int k) {
+ int f[n + 1][k + 1][2];
+ memset(f, 0, sizeof(f));
+ f[0][0][1] = 1;
+ const int mod = 1e9 + 7;
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ for (int i = 1; i <= n; ++i) {
+ for (int j = 0; j <= k; ++j) {
+ f[i][j][0] = 1LL * (f[i - 1][j][1] + (j ? f[i - 1][j - 1][0] : 0)) * cnt0 % mod;
+ f[i][j][1] = 1LL * (f[i - 1][j][0] + f[i - 1][j][1]) * cnt1 % mod;
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod;
+ }
+};
+```
+
+#### Go
+
+```go
+func countOfArrays(n int, m int, k int) int {
+ f := make([][][2]int, n+1)
+ for i := range f {
+ f[i] = make([][2]int, k+1)
+ }
+ f[0][0][1] = 1
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ const mod int = 1e9 + 7
+ for i := 1; i <= n; i++ {
+ for j := 0; j <= k; j++ {
+ f[i][j][0] = cnt0 * f[i-1][j][1] % mod
+ if j > 0 {
+ f[i][j][0] = (f[i][j][0] + cnt0*f[i-1][j-1][0]%mod) % mod
+ }
+ f[i][j][1] = cnt1 * (f[i-1][j][0] + f[i-1][j][1]) % mod
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod
+}
+```
+
+#### TypeScript
+
+```ts
+function countOfArrays(n: number, m: number, k: number): number {
+ const f: number[][][] = Array.from({ length: n + 1 }, () =>
+ Array.from({ length: k + 1 }, () => Array(2).fill(0)),
+ );
+ f[0][0][1] = 1;
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ for (let i = 1; i <= n; ++i) {
+ for (let j = 0; j <= k; ++j) {
+ f[i][j][0] = (cnt0 * (f[i - 1][j][1] + (j ? f[i - 1][j - 1][0] : 0))) % mod;
+ f[i][j][1] = (cnt1 * (f[i - 1][j][0] + f[i - 1][j][1])) % mod;
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod;
+}
+```
+
+
+
+
+
+
+
+### Solution 3: Dynamic Programming (Space Optimization)
+
+We observe that the computation of $f[i]$ only depends on $f[i - 1]$, allowing us to optimize the space usage with a rolling array.
+
+The time complexity is $O(n \times k)$, and the space complexity is $O(k)$, where $n$ and $k$ are the parameters given in the problem.
+
+
+
+#### Python3
+
+```python
+class Solution:
+ def countOfArrays(self, n: int, m: int, k: int) -> int:
+ f = [[0] * 2 for _ in range(k + 1)]
+ cnt0 = m // 2
+ cnt1 = m - cnt0
+ mod = 10**9 + 7
+ f[0][1] = 1
+ for _ in range(n):
+ g = [[0] * 2 for _ in range(k + 1)]
+ for j in range(k + 1):
+ g[j][0] = (f[j][1] + (f[j - 1][0] if j else 0)) * cnt0 % mod
+ g[j][1] = (f[j][0] + f[j][1]) * cnt1 % mod
+ f = g
+ return sum(f[k]) % mod
+```
+
+#### Java
+
+```java
+class Solution {
+ public int countOfArrays(int n, int m, int k) {
+ int[][] f = new int[k + 1][2];
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ final int mod = (int) 1e9 + 7;
+ f[0][1] = 1;
+ for (int i = 0; i < n; ++i) {
+ int[][] g = new int[k + 1][2];
+ for (int j = 0; j <= k; ++j) {
+ g[j][0] = (int) (1L * cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0)) % mod);
+ g[j][1] = (int) (1L * cnt1 * (f[j][0] + f[j][1]) % mod);
+ }
+ f = g;
+ }
+ return (f[k][0] + f[k][1]) % mod;
+ }
+}
+```
+
+#### C++
+
+```cpp
+class Solution {
+public:
+ int countOfArrays(int n, int m, int k) {
+ vector> f(k + 1, vector(2));
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ const int mod = 1e9 + 7;
+ f[0][1] = 1;
+
+ for (int i = 0; i < n; ++i) {
+ vector> g(k + 1, vector(2));
+ for (int j = 0; j <= k; ++j) {
+ g[j][0] = (1LL * cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0)) % mod) % mod;
+ g[j][1] = (1LL * cnt1 * (f[j][0] + f[j][1]) % mod) % mod;
+ }
+ f = g;
+ }
+ return (f[k][0] + f[k][1]) % mod;
+ }
+};
+```
+
+#### Go
+
+```go
+func countOfArrays(n int, m int, k int) int {
+ const mod = 1e9 + 7
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ f := make([][2]int, k+1)
+ f[0][1] = 1
+
+ for i := 0; i < n; i++ {
+ g := make([][2]int, k+1)
+ for j := 0; j <= k; j++ {
+ g[j][0] = (cnt0 * (f[j][1] + func() int {
+ if j > 0 {
+ return f[j-1][0]
+ }
+ return 0
+ }()) % mod) % mod
+ g[j][1] = (cnt1 * (f[j][0] + f[j][1]) % mod) % mod
+ }
+ f = g
+ }
+
+ return (f[k][0] + f[k][1]) % mod
+}
+```
+
+#### TypeScript
+
+```ts
+function countOfArrays(n: number, m: number, k: number): number {
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ const f: number[][] = Array.from({ length: k + 1 }, () => [0, 0]);
+ f[0][1] = 1;
+ for (let i = 0; i < n; i++) {
+ const g: number[][] = Array.from({ length: k + 1 }, () => [0, 0]);
+ for (let j = 0; j <= k; j++) {
+ g[j][0] = ((cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0))) % mod) % mod;
+ g[j][1] = ((cnt1 * (f[j][0] + f[j][1])) % mod) % mod;
+ }
+ f.splice(0, f.length, ...g);
+ }
+ return (f[k][0] + f[k][1]) % mod;
+}
+```
+
+
+
+
+
+
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.cpp b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.cpp
new file mode 100644
index 0000000000000..c63fc10ccfb94
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.cpp
@@ -0,0 +1,25 @@
+class Solution {
+public:
+ int countOfArrays(int n, int m, int k) {
+ int f[n][k + 1][2];
+ memset(f, -1, sizeof(f));
+ const int mod = 1e9 + 7;
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ auto dfs = [&](auto&& dfs, int i, int j, int k) -> int {
+ if (j < 0) {
+ return 0;
+ }
+ if (i >= n) {
+ return j == 0 ? 1 : 0;
+ }
+ if (f[i][j][k] != -1) {
+ return f[i][j][k];
+ }
+ int a = 1LL * cnt1 * dfs(dfs, i + 1, j, 1) % mod;
+ int b = 1LL * cnt0 * dfs(dfs, i + 1, j - (k & 1 ^ 1), 0) % mod;
+ return f[i][j][k] = (a + b) % mod;
+ };
+ return dfs(dfs, 0, k, 1);
+ }
+};
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.go b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.go
new file mode 100644
index 0000000000000..570572586e646
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.go
@@ -0,0 +1,32 @@
+func countOfArrays(n int, m int, k int) int {
+ f := make([][][2]int, n)
+ for i := range f {
+ f[i] = make([][2]int, k+1)
+ for j := range f[i] {
+ f[i][j] = [2]int{-1, -1}
+ }
+ }
+ const mod int = 1e9 + 7
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ var dfs func(int, int, int) int
+ dfs = func(i, j, k int) int {
+ if j < 0 {
+ return 0
+ }
+ if i >= n {
+ if j == 0 {
+ return 1
+ }
+ return 0
+ }
+ if f[i][j][k] != -1 {
+ return f[i][j][k]
+ }
+ a := cnt1 * dfs(i+1, j, 1) % mod
+ b := cnt0 * dfs(i+1, j-(k&1^1), 0) % mod
+ f[i][j][k] = (a + b) % mod
+ return f[i][j][k]
+ }
+ return dfs(0, k, 1)
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.java b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.java
new file mode 100644
index 0000000000000..fc956359a0ffe
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.java
@@ -0,0 +1,27 @@
+class Solution {
+ private Integer[][][] f;
+ private long cnt0, cnt1;
+ private final int mod = (int) 1e9 + 7;
+
+ public int countOfArrays(int n, int m, int k) {
+ f = new Integer[n][k + 1][2];
+ cnt0 = m / 2;
+ cnt1 = m - cnt0;
+ return dfs(0, k, 1);
+ }
+
+ private int dfs(int i, int j, int k) {
+ if (j < 0) {
+ return 0;
+ }
+ if (i >= f.length) {
+ return j == 0 ? 1 : 0;
+ }
+ if (f[i][j][k] != null) {
+ return f[i][j][k];
+ }
+ int a = (int) (cnt1 * dfs(i + 1, j, 1) % mod);
+ int b = (int) (cnt0 * dfs(i + 1, j - (k & 1 ^ 1), 0) % mod);
+ return f[i][j][k] = (a + b) % mod;
+ }
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.py b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.py
new file mode 100644
index 0000000000000..f18629b61e594
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.py
@@ -0,0 +1,18 @@
+class Solution:
+ def countOfArrays(self, n: int, m: int, k: int) -> int:
+ @cache
+ def dfs(i: int, j: int, k: int) -> int:
+ if j < 0:
+ return 0
+ if i >= n:
+ return int(j == 0)
+ return (
+ cnt1 * dfs(i + 1, j, 1) + cnt0 * dfs(i + 1, j - (k & 1 ^ 1), 0)
+ ) % mod
+
+ cnt0 = m // 2
+ cnt1 = m - cnt0
+ mod = 10**9 + 7
+ ans = dfs(0, k, 1)
+ dfs.cache_clear()
+ return ans
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.ts b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.ts
new file mode 100644
index 0000000000000..0efe1ff42980a
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution.ts
@@ -0,0 +1,23 @@
+function countOfArrays(n: number, m: number, k: number): number {
+ const f = Array.from({ length: n }, () =>
+ Array.from({ length: k + 1 }, () => Array(2).fill(-1)),
+ );
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ const dfs = (i: number, j: number, k: number): number => {
+ if (j < 0) {
+ return 0;
+ }
+ if (i >= n) {
+ return j === 0 ? 1 : 0;
+ }
+ if (f[i][j][k] !== -1) {
+ return f[i][j][k];
+ }
+ const a = (cnt1 * dfs(i + 1, j, 1)) % mod;
+ const b = (cnt0 * dfs(i + 1, j - ((k & 1) ^ 1), 0)) % mod;
+ return (f[i][j][k] = (a + b) % mod);
+ };
+ return dfs(0, k, 1);
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.cpp b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.cpp
new file mode 100644
index 0000000000000..7264eccf25e50
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.cpp
@@ -0,0 +1,18 @@
+class Solution {
+public:
+ int countOfArrays(int n, int m, int k) {
+ int f[n + 1][k + 1][2];
+ memset(f, 0, sizeof(f));
+ f[0][0][1] = 1;
+ const int mod = 1e9 + 7;
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ for (int i = 1; i <= n; ++i) {
+ for (int j = 0; j <= k; ++j) {
+ f[i][j][0] = 1LL * (f[i - 1][j][1] + (j ? f[i - 1][j - 1][0] : 0)) * cnt0 % mod;
+ f[i][j][1] = 1LL * (f[i - 1][j][0] + f[i - 1][j][1]) * cnt1 % mod;
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod;
+ }
+};
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.go b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.go
new file mode 100644
index 0000000000000..6b2410be87167
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.go
@@ -0,0 +1,20 @@
+func countOfArrays(n int, m int, k int) int {
+ f := make([][][2]int, n+1)
+ for i := range f {
+ f[i] = make([][2]int, k+1)
+ }
+ f[0][0][1] = 1
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ const mod int = 1e9 + 7
+ for i := 1; i <= n; i++ {
+ for j := 0; j <= k; j++ {
+ f[i][j][0] = cnt0 * f[i-1][j][1] % mod
+ if j > 0 {
+ f[i][j][0] = (f[i][j][0] + cnt0*f[i-1][j-1][0]%mod) % mod
+ }
+ f[i][j][1] = cnt1 * (f[i-1][j][0] + f[i-1][j][1]) % mod
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.java b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.java
new file mode 100644
index 0000000000000..d7a6f59da2993
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.java
@@ -0,0 +1,17 @@
+class Solution {
+ public int countOfArrays(int n, int m, int k) {
+ int[][][] f = new int[n + 1][k + 1][2];
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ final int mod = (int) 1e9 + 7;
+ f[0][0][1] = 1;
+ for (int i = 1; i <= n; ++i) {
+ for (int j = 0; j <= k; ++j) {
+ f[i][j][0]
+ = (int) (1L * cnt0 * (f[i - 1][j][1] + (j > 0 ? f[i - 1][j - 1][0] : 0)) % mod);
+ f[i][j][1] = (int) (1L * cnt1 * (f[i - 1][j][0] + f[i - 1][j][1]) % mod);
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod;
+ }
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.py b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.py
new file mode 100644
index 0000000000000..71b845cb7470e
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.py
@@ -0,0 +1,14 @@
+class Solution:
+ def countOfArrays(self, n: int, m: int, k: int) -> int:
+ f = [[[0] * 2 for _ in range(k + 1)] for _ in range(n + 1)]
+ cnt0 = m // 2
+ cnt1 = m - cnt0
+ mod = 10**9 + 7
+ f[0][0][1] = 1
+ for i in range(1, n + 1):
+ for j in range(k + 1):
+ f[i][j][0] = (
+ (f[i - 1][j][1] + (f[i - 1][j - 1][0] if j else 0)) * cnt0 % mod
+ )
+ f[i][j][1] = (f[i - 1][j][0] + f[i - 1][j][1]) * cnt1 % mod
+ return sum(f[n][k]) % mod
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.ts b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.ts
new file mode 100644
index 0000000000000..c204efc739d4f
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution2.ts
@@ -0,0 +1,16 @@
+function countOfArrays(n: number, m: number, k: number): number {
+ const f: number[][][] = Array.from({ length: n + 1 }, () =>
+ Array.from({ length: k + 1 }, () => Array(2).fill(0)),
+ );
+ f[0][0][1] = 1;
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ for (let i = 1; i <= n; ++i) {
+ for (let j = 0; j <= k; ++j) {
+ f[i][j][0] = (cnt0 * (f[i - 1][j][1] + (j ? f[i - 1][j - 1][0] : 0))) % mod;
+ f[i][j][1] = (cnt1 * (f[i - 1][j][0] + f[i - 1][j][1])) % mod;
+ }
+ }
+ return (f[n][k][0] + f[n][k][1]) % mod;
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.cpp b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.cpp
new file mode 100644
index 0000000000000..6dfd427b0fa77
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.cpp
@@ -0,0 +1,20 @@
+class Solution {
+public:
+ int countOfArrays(int n, int m, int k) {
+ vector> f(k + 1, vector(2));
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ const int mod = 1e9 + 7;
+ f[0][1] = 1;
+
+ for (int i = 0; i < n; ++i) {
+ vector> g(k + 1, vector(2));
+ for (int j = 0; j <= k; ++j) {
+ g[j][0] = (1LL * cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0)) % mod) % mod;
+ g[j][1] = (1LL * cnt1 * (f[j][0] + f[j][1]) % mod) % mod;
+ }
+ f = g;
+ }
+ return (f[k][0] + f[k][1]) % mod;
+ }
+};
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.go b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.go
new file mode 100644
index 0000000000000..fba0343c7f419
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.go
@@ -0,0 +1,23 @@
+func countOfArrays(n int, m int, k int) int {
+ const mod = 1e9 + 7
+ cnt0 := m / 2
+ cnt1 := m - cnt0
+ f := make([][2]int, k+1)
+ f[0][1] = 1
+
+ for i := 0; i < n; i++ {
+ g := make([][2]int, k+1)
+ for j := 0; j <= k; j++ {
+ g[j][0] = (cnt0 * (f[j][1] + func() int {
+ if j > 0 {
+ return f[j-1][0]
+ }
+ return 0
+ }()) % mod) % mod
+ g[j][1] = (cnt1 * (f[j][0] + f[j][1]) % mod) % mod
+ }
+ f = g
+ }
+
+ return (f[k][0] + f[k][1]) % mod
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.java b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.java
new file mode 100644
index 0000000000000..cee3924a2d576
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.java
@@ -0,0 +1,18 @@
+class Solution {
+ public int countOfArrays(int n, int m, int k) {
+ int[][] f = new int[k + 1][2];
+ int cnt0 = m / 2;
+ int cnt1 = m - cnt0;
+ final int mod = (int) 1e9 + 7;
+ f[0][1] = 1;
+ for (int i = 0; i < n; ++i) {
+ int[][] g = new int[k + 1][2];
+ for (int j = 0; j <= k; ++j) {
+ g[j][0] = (int) (1L * cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0)) % mod);
+ g[j][1] = (int) (1L * cnt1 * (f[j][0] + f[j][1]) % mod);
+ }
+ f = g;
+ }
+ return (f[k][0] + f[k][1]) % mod;
+ }
+}
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.py b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.py
new file mode 100644
index 0000000000000..fb82cbeca1d57
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.py
@@ -0,0 +1,14 @@
+class Solution:
+ def countOfArrays(self, n: int, m: int, k: int) -> int:
+ f = [[0] * 2 for _ in range(k + 1)]
+ cnt0 = m // 2
+ cnt1 = m - cnt0
+ mod = 10**9 + 7
+ f[0][1] = 1
+ for _ in range(n):
+ g = [[0] * 2 for _ in range(k + 1)]
+ for j in range(k + 1):
+ g[j][0] = (f[j][1] + (f[j - 1][0] if j else 0)) * cnt0 % mod
+ g[j][1] = (f[j][0] + f[j][1]) * cnt1 % mod
+ f = g
+ return sum(f[k]) % mod
diff --git a/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.ts b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.ts
new file mode 100644
index 0000000000000..05c4f32b4de71
--- /dev/null
+++ b/solution/3300-3399/3339.Find the Number of K-Even Arrays/Solution3.ts
@@ -0,0 +1,16 @@
+function countOfArrays(n: number, m: number, k: number): number {
+ const mod = 1e9 + 7;
+ const cnt0 = Math.floor(m / 2);
+ const cnt1 = m - cnt0;
+ const f: number[][] = Array.from({ length: k + 1 }, () => [0, 0]);
+ f[0][1] = 1;
+ for (let i = 0; i < n; i++) {
+ const g: number[][] = Array.from({ length: k + 1 }, () => [0, 0]);
+ for (let j = 0; j <= k; j++) {
+ g[j][0] = ((cnt0 * (f[j][1] + (j > 0 ? f[j - 1][0] : 0))) % mod) % mod;
+ g[j][1] = ((cnt1 * (f[j][0] + f[j][1])) % mod) % mod;
+ }
+ f.splice(0, f.length, ...g);
+ }
+ return (f[k][0] + f[k][1]) % mod;
+}
diff --git a/solution/README.md b/solution/README.md
index b618c5fb3d1fe..8b0797d94f9a2 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -3349,6 +3349,7 @@
| 3336 | [最大公约数相等的子序列数量](/solution/3300-3399/3336.Find%20the%20Number%20of%20Subsequences%20With%20Equal%20GCD/README.md) | `数组`,`数学`,`动态规划`,`数论` | 困难 | 第 421 场周赛 |
| 3337 | [字符串转换后的长度 II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README.md) | `哈希表`,`数学`,`字符串`,`动态规划`,`计数` | 困难 | 第 421 场周赛 |
| 3338 | [第二高的薪水 II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |
+| 3339 | [查找 K 偶数数组的数量](/solution/3300-3399/3339.Find%20the%20Number%20of%20K-Even%20Arrays/README.md) | `动态规划` | 中等 | 🔒 |
## 版权
@@ -3359,4 +3360,4 @@
欢迎各位小伙伴们添加 @yanglbme 的个人微信(微信号:YLB0109),备注 「**leetcode**」。后续我们会创建算法、技术相关的交流群,大家一起交流学习,分享经验,共同进步。
|
|
-| ------------------------------------------------------------------------------------------------------------------------------ |
\ No newline at end of file
+| ------------------------------------------------------------------------------------------------------------------------------ |
diff --git a/solution/README_EN.md b/solution/README_EN.md
index 88d6eed6b164e..65cebfefc8c83 100644
--- a/solution/README_EN.md
+++ b/solution/README_EN.md
@@ -3347,6 +3347,7 @@ Press Control + F(or Command + F on
| 3336 | [Find the Number of Subsequences With Equal GCD](/solution/3300-3399/3336.Find%20the%20Number%20of%20Subsequences%20With%20Equal%20GCD/README_EN.md) | `Array`,`Math`,`Dynamic Programming`,`Number Theory` | Hard | Weekly Contest 421 |
| 3337 | [Total Characters in String After Transformations II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README_EN.md) | `Hash Table`,`Math`,`String`,`Dynamic Programming`,`Counting` | Hard | Weekly Contest 421 |
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |
+| 3339 | [Find the Number of K-Even Arrays](/solution/3300-3399/3339.Find%20the%20Number%20of%20K-Even%20Arrays/README_EN.md) | `Dynamic Programming` | Medium | 🔒 |
## Copyright