diff --git a/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md b/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md index 23fc810971705..7c5a8b3e828d1 100644 --- a/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md +++ b/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md @@ -155,13 +155,13 @@ func numberOfSubarrays(nums []int, k int) (ans int) { ```ts function numberOfSubarrays(nums: number[], k: number): number { const n = nums.length; - const cnt = new Array(n + 1).fill(0); + const cnt = Array(n + 1).fill(0); cnt[0] = 1; let ans = 0; let t = 0; for (const v of nums) { t += v & 1; - if (t - k >= 0) { + if (t >= k) { ans += cnt[t - k]; } cnt[t] += 1; diff --git a/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md b/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md index d8e0702f29b10..492b0e35d7a75 100644 --- a/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md +++ b/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md @@ -153,13 +153,13 @@ func numberOfSubarrays(nums []int, k int) (ans int) { ```ts function numberOfSubarrays(nums: number[], k: number): number { const n = nums.length; - const cnt = new Array(n + 1).fill(0); + const cnt = Array(n + 1).fill(0); cnt[0] = 1; let ans = 0; let t = 0; for (const v of nums) { t += v & 1; - if (t - k >= 0) { + if (t >= k) { ans += cnt[t - k]; } cnt[t] += 1; diff --git a/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts b/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts index 9e42b5ddd9645..4a6eaa498b4df 100644 --- a/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts +++ b/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts @@ -1,12 +1,12 @@ function numberOfSubarrays(nums: number[], k: number): number { const n = nums.length; - const cnt = new Array(n + 1).fill(0); + const cnt = Array(n + 1).fill(0); cnt[0] = 1; let ans = 0; let t = 0; for (const v of nums) { t += v & 1; - if (t - k >= 0) { + if (t >= k) { ans += cnt[t - k]; } cnt[t] += 1; diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md index f78096f57e547..ccc4d129ec2fa 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md @@ -80,11 +80,11 @@ tags: ### 方法一:模拟 -创建一个矩阵 $g$ 来存放操作的结果。对于 $indices$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。 +我们创建一个矩阵 $g$ 来存放操作的结果。对于 $\text{indices}$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。 模拟结束后,遍历矩阵,统计奇数的个数。 -时间复杂度 $O(indices.length*(m+n)+mn)$,空间复杂度 $O(mn)$。 +时间复杂度 $O(k \times (m + n) + m \times n)$,空间复杂度 $O(m \times n)$。其中 $k$ 为 $\text{indices}$ 的长度。 @@ -137,12 +137,19 @@ public: vector> g(m, vector(n)); for (auto& e : indices) { int r = e[0], c = e[1]; - for (int i = 0; i < m; ++i) ++g[i][c]; - for (int j = 0; j < n; ++j) ++g[r][j]; + for (int i = 0; i < m; ++i) { + ++g[i][c]; + } + for (int j = 0; j < n; ++j) { + ++g[r][j]; + } } int ans = 0; - for (auto& row : g) - for (int v : row) ans += v % 2; + for (auto& row : g) { + for (int v : row) { + ans += v % 2; + } + } return ans; } }; @@ -183,11 +190,11 @@ func oddCells(m int, n int, indices [][]int) int { ### 方法二:空间优化 -用行数组 $row$ 和列数组 $col$ 来记录每一行、每一列被增加的次数。对于 $indices$ 中的每一对 $(r_i, c_i)$,我们将 $row[r_i]$ 和 $col[c_i]$ 分别加 $1$。 +我们可以使用行数组 $\text{row}$ 和列数组 $\text{col}$ 来记录每一行、每一列被增加的次数。对于 $\text{indices}$ 中的每一对 $(r_i, c_i)$,我们将 $\text{row}[r_i]$ 和 $\text{col}[c_i]$ 分别加 $1$。 -操作结束后,可以算出 $(i, j)$ 位置的计数为 $row[i]+col[j]$。遍历矩阵,统计奇数的个数。 +操作结束后,可以算出 $(i, j)$ 位置的计数为 $\text{row}[i]+\text{col}[j]$。遍历矩阵,统计奇数的个数。 -时间复杂度 $O(indices.length+mn)$,空间复杂度 $O(m+n)$。 +时间复杂度 $O(k + m \times n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\text{indices}$ 的长度。 @@ -241,8 +248,11 @@ public: col[c]++; } int ans = 0; - for (int i : row) - for (int j : col) ans += (i + j) % 2; + for (int i : row) { + for (int j : col) { + ans += (i + j) % 2; + } + } return ans; } }; @@ -277,11 +287,11 @@ func oddCells(m int, n int, indices [][]int) int { ### 方法三:数学优化 -我们注意到,只有当 $row[i]$ 和 $col[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。 +我们注意到,只有当 $\text{row}[i]$ 和 $\text{col}[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。 -我们统计 $row$ 中的奇数个数,记为 $cnt1$;$col$ 中的奇数个数,记为 $cnt2$。那么最终得到的奇数个数为 $cnt1*(n-cnt2)+cnt2*(m-cnt1)$。 +我们统计 $\text{row}$ 中的奇数个数,记为 $\text{cnt1}$;而 $\text{col}$ 中的奇数个数,记为 $\text{cnt2}$。那么最终得到的奇数个数为 $\text{cnt1} \times (n - \text{cnt2}) + \text{cnt2} \times (m - \text{cnt1})$。 -时间复杂度 $O(indices.length+m+n)$,空间复杂度 $O(m+n)$。 +时间复杂度 $O(k + m + n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\text{indices}$ 的长度。 @@ -338,8 +348,12 @@ public: col[c]++; } int cnt1 = 0, cnt2 = 0; - for (int v : row) cnt1 += v % 2; - for (int v : col) cnt2 += v % 2; + for (int v : row) { + cnt1 += v % 2; + } + for (int v : col) { + cnt2 += v % 2; + } return cnt1 * (n - cnt2) + cnt2 * (m - cnt1); } }; diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md index bf010312726dd..0f77e493498a6 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md @@ -71,11 +71,11 @@ The final matrix is [[1,3,1],[1,3,1]], which contains 6 odd numbers. ### Solution 1: Simulation -We create a matrix $g$ to store the results of the operations. For each pair $(r_i, c_i)$ in $indices$, we add $1$ to all elements in the $r_i$th row and the $c_i$th column of the matrix. +We create a matrix $g$ to store the result of operations. For each pair $(r_i, c_i)$ in $\text{indices}$, we add $1$ to all numbers in the $r_i$-th row of the matrix and add $1$ to all elements in the $c_i$-th column. -After the simulation, we traverse the matrix and count the number of odd numbers. +After the simulation ends, we traverse the matrix and count the number of odd numbers. -The time complexity is $O(\text{indices.length} \times (m+n) + mn)$, and the space complexity is $O(mn)$. +The time complexity is $O(k \times (m + n) + m \times n)$, and the space complexity is $O(m \times n)$. Here, $k$ is the length of $\text{indices}$. @@ -128,12 +128,19 @@ public: vector> g(m, vector(n)); for (auto& e : indices) { int r = e[0], c = e[1]; - for (int i = 0; i < m; ++i) ++g[i][c]; - for (int j = 0; j < n; ++j) ++g[r][j]; + for (int i = 0; i < m; ++i) { + ++g[i][c]; + } + for (int j = 0; j < n; ++j) { + ++g[r][j]; + } } int ans = 0; - for (auto& row : g) - for (int v : row) ans += v % 2; + for (auto& row : g) { + for (int v : row) { + ans += v % 2; + } + } return ans; } }; @@ -174,11 +181,11 @@ func oddCells(m int, n int, indices [][]int) int { ### Solution 2: Space Optimization -We use row array $row$ and column array $col$ to record the number of times each row and column are increased. For each pair $(r_i, c_i)$ in $indices$, we add $1$ to $row[r_i]$ and $col[c_i]$ respectively. +We can use a row array $\text{row}$ and a column array $\text{col}$ to record the number of times each row and column is incremented. For each pair $(r_i, c_i)$ in $\text{indices}$, we add $1$ to $\text{row}[r_i]$ and $\text{col}[c_i]$ respectively. -After the operation, we can calculate that the count at position $(i, j)$ is $row[i] + col[j]$. We traverse the matrix and count the number of odd numbers. +After the operations are completed, the count at position $(i, j)$ can be calculated as $\text{row}[i] + \text{col}[j]$. We traverse the matrix and count the number of odd numbers. -The time complexity is $O(\text{indices.length} + mn)$, and the space complexity is $O(m+n)$. +The time complexity is $O(k + m \times n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\text{indices}$. @@ -232,8 +239,11 @@ public: col[c]++; } int ans = 0; - for (int i : row) - for (int j : col) ans += (i + j) % 2; + for (int i : row) { + for (int j : col) { + ans += (i + j) % 2; + } + } return ans; } }; @@ -268,11 +278,11 @@ func oddCells(m int, n int, indices [][]int) int { ### Solution 3: Mathematical Optimization -We notice that only when exactly one of $row[i]$ and $col[j]$ is odd, the number at position $(i, j)$ in the matrix will be odd. +We notice that a number at position $(i, j)$ in the matrix will be odd only when exactly one of $\text{row}[i]$ and $\text{col}[j]$ is odd and the other is even. -We count the number of odd numbers in $row$, denoted as $cnt1$; the number of odd numbers in $col$, denoted as $cnt2$. Then the final number of odd numbers is $cnt1 \times (n-cnt2) + cnt2 \times (m-cnt1)$. +We count the number of odd numbers in $\text{row}$, denoted as $\text{cnt1}$, and the number of odd numbers in $\text{col}$, denoted as $\text{cnt2}$. Therefore, the final count of odd numbers is $\text{cnt1} \times (n - \text{cnt2}) + \text{cnt2} \times (m - \text{cnt1})$. -The time complexity is $O(\text{indices.length} + m + n)$, and the space complexity is $O(m+n)$. +The time complexity is $O(k + m + n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\text{indices}$. @@ -329,8 +339,12 @@ public: col[c]++; } int cnt1 = 0, cnt2 = 0; - for (int v : row) cnt1 += v % 2; - for (int v : col) cnt2 += v % 2; + for (int v : row) { + cnt1 += v % 2; + } + for (int v : col) { + cnt2 += v % 2; + } return cnt1 * (n - cnt2) + cnt2 * (m - cnt1); } }; diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp index 4408697fe038b..0083e28920e2a 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp @@ -4,12 +4,19 @@ class Solution { vector> g(m, vector(n)); for (auto& e : indices) { int r = e[0], c = e[1]; - for (int i = 0; i < m; ++i) ++g[i][c]; - for (int j = 0; j < n; ++j) ++g[r][j]; + for (int i = 0; i < m; ++i) { + ++g[i][c]; + } + for (int j = 0; j < n; ++j) { + ++g[r][j]; + } } int ans = 0; - for (auto& row : g) - for (int v : row) ans += v % 2; + for (auto& row : g) { + for (int v : row) { + ans += v % 2; + } + } return ans; } }; \ No newline at end of file diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution2.cpp b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution2.cpp index 68dddddc2c4ab..ed8c41d7f6d64 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution2.cpp +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution2.cpp @@ -9,8 +9,11 @@ class Solution { col[c]++; } int ans = 0; - for (int i : row) - for (int j : col) ans += (i + j) % 2; + for (int i : row) { + for (int j : col) { + ans += (i + j) % 2; + } + } return ans; } }; \ No newline at end of file diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution3.cpp b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution3.cpp index e648b4d8b635f..12c3d85ee2e08 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution3.cpp +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution3.cpp @@ -9,8 +9,12 @@ class Solution { col[c]++; } int cnt1 = 0, cnt2 = 0; - for (int v : row) cnt1 += v % 2; - for (int v : col) cnt2 += v % 2; + for (int v : row) { + cnt1 += v % 2; + } + for (int v : col) { + cnt2 += v % 2; + } return cnt1 * (n - cnt2) + cnt2 * (m - cnt1); } }; \ No newline at end of file