Skip to content

feat: update solutions to lc problems: No.1248,1252 #3144

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
Jun 22, 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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
46 changes: 30 additions & 16 deletions solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}$ 的长度

<!-- tabs:start -->

Expand Down Expand Up @@ -137,12 +137,19 @@ public:
vector<vector<int>> g(m, vector<int>(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;
}
};
Expand Down Expand Up @@ -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}$ 的长度

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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}$ 的长度

<!-- tabs:start -->

Expand Down Expand Up @@ -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);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -128,12 +128,19 @@ public:
vector<vector<int>> g(m, vector<int>(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;
}
};
Expand Down Expand Up @@ -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}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ class Solution {
vector<vector<int>> g(m, vector<int>(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;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Loading