Skip to content

Commit 04cfc47

Browse files
committed
feat: update solutions to lc problems: No.1248,1252
* No.1248.Count Number of Nice Subarrays * No.1252.Cells with Odd Values in a Matrix
1 parent 76e854a commit 04cfc47

File tree

8 files changed

+89
-47
lines changed

8 files changed

+89
-47
lines changed

solution/1200-1299/1248.Count Number of Nice Subarrays/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ func numberOfSubarrays(nums []int, k int) (ans int) {
155155
```ts
156156
function numberOfSubarrays(nums: number[], k: number): number {
157157
const n = nums.length;
158-
const cnt = new Array(n + 1).fill(0);
158+
const cnt = Array(n + 1).fill(0);
159159
cnt[0] = 1;
160160
let ans = 0;
161161
let t = 0;
162162
for (const v of nums) {
163163
t += v & 1;
164-
if (t - k >= 0) {
164+
if (t >= k) {
165165
ans += cnt[t - k];
166166
}
167167
cnt[t] += 1;

solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ func numberOfSubarrays(nums []int, k int) (ans int) {
153153
```ts
154154
function numberOfSubarrays(nums: number[], k: number): number {
155155
const n = nums.length;
156-
const cnt = new Array(n + 1).fill(0);
156+
const cnt = Array(n + 1).fill(0);
157157
cnt[0] = 1;
158158
let ans = 0;
159159
let t = 0;
160160
for (const v of nums) {
161161
t += v & 1;
162-
if (t - k >= 0) {
162+
if (t >= k) {
163163
ans += cnt[t - k];
164164
}
165165
cnt[t] += 1;

solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function numberOfSubarrays(nums: number[], k: number): number {
22
const n = nums.length;
3-
const cnt = new Array(n + 1).fill(0);
3+
const cnt = Array(n + 1).fill(0);
44
cnt[0] = 1;
55
let ans = 0;
66
let t = 0;
77
for (const v of nums) {
88
t += v & 1;
9-
if (t - k >= 0) {
9+
if (t >= k) {
1010
ans += cnt[t - k];
1111
}
1212
cnt[t] += 1;

solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ tags:
8080

8181
### 方法一:模拟
8282

83-
创建一个矩阵 $g$ 来存放操作的结果。对于 $indices$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。
83+
我们创建一个矩阵 $g$ 来存放操作的结果。对于 $\text{indices}$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。
8484

8585
模拟结束后,遍历矩阵,统计奇数的个数。
8686

87-
时间复杂度 $O(indices.length*(m+n)+mn)$,空间复杂度 $O(mn)$
87+
时间复杂度 $O(k \times (m + n) + m \times n)$,空间复杂度 $O(m \times n)$。其中 $k$ 为 $\text{indices}$ 的长度
8888

8989
<!-- tabs:start -->
9090

@@ -137,12 +137,19 @@ public:
137137
vector<vector<int>> g(m, vector<int>(n));
138138
for (auto& e : indices) {
139139
int r = e[0], c = e[1];
140-
for (int i = 0; i < m; ++i) ++g[i][c];
141-
for (int j = 0; j < n; ++j) ++g[r][j];
140+
for (int i = 0; i < m; ++i) {
141+
++g[i][c];
142+
}
143+
for (int j = 0; j < n; ++j) {
144+
++g[r][j];
145+
}
142146
}
143147
int ans = 0;
144-
for (auto& row : g)
145-
for (int v : row) ans += v % 2;
148+
for (auto& row : g) {
149+
for (int v : row) {
150+
ans += v % 2;
151+
}
152+
}
146153
return ans;
147154
}
148155
};
@@ -183,11 +190,11 @@ func oddCells(m int, n int, indices [][]int) int {
183190

184191
### 方法二:空间优化
185192

186-
用行数组 $row$ 和列数组 $col$ 来记录每一行、每一列被增加的次数。对于 $indices$ 中的每一对 $(r_i, c_i)$,我们将 $row[r_i]$ 和 $col[c_i]$ 分别加 $1$。
193+
我们可以使用行数组 $\text{row}$ 和列数组 $\text{col}$ 来记录每一行、每一列被增加的次数。对于 $\text{indices}$ 中的每一对 $(r_i, c_i)$,我们将 $\text{row}[r_i]$ 和 $\text{col}[c_i]$ 分别加 $1$。
187194

188-
操作结束后,可以算出 $(i, j)$ 位置的计数为 $row[i]+col[j]$。遍历矩阵,统计奇数的个数。
195+
操作结束后,可以算出 $(i, j)$ 位置的计数为 $\text{row}[i]+\text{col}[j]$。遍历矩阵,统计奇数的个数。
189196

190-
时间复杂度 $O(indices.length+mn)$,空间复杂度 $O(m+n)$。
197+
时间复杂度 $O(k + m \times n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\text{indices}$ 的长度
191198

192199
<!-- tabs:start -->
193200

@@ -241,8 +248,11 @@ public:
241248
col[c]++;
242249
}
243250
int ans = 0;
244-
for (int i : row)
245-
for (int j : col) ans += (i + j) % 2;
251+
for (int i : row) {
252+
for (int j : col) {
253+
ans += (i + j) % 2;
254+
}
255+
}
246256
return ans;
247257
}
248258
};
@@ -277,11 +287,11 @@ func oddCells(m int, n int, indices [][]int) int {
277287

278288
### 方法三:数学优化
279289

280-
我们注意到,只有当 $row[i]$ 和 $col[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。
290+
我们注意到,只有当 $\text{row}[i]$ 和 $\text{col}[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。
281291

282-
我们统计 $row$ 中的奇数个数,记为 $cnt1$;$col$ 中的奇数个数,记为 $cnt2$。那么最终得到的奇数个数为 $cnt1*(n-cnt2)+cnt2*(m-cnt1)$。
292+
我们统计 $\text{row}$ 中的奇数个数,记为 $\text{cnt1}$;而 $\text{col}$ 中的奇数个数,记为 $\text{cnt2}$。那么最终得到的奇数个数为 $\text{cnt1} \times (n - \text{cnt2}) + \text{cnt2} \times (m - \text{cnt1})$。
283293

284-
时间复杂度 $O(indices.length+m+n)$,空间复杂度 $O(m+n)$。
294+
时间复杂度 $O(k + m + n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\text{indices}$ 的长度
285295

286296
<!-- tabs:start -->
287297

@@ -338,8 +348,12 @@ public:
338348
col[c]++;
339349
}
340350
int cnt1 = 0, cnt2 = 0;
341-
for (int v : row) cnt1 += v % 2;
342-
for (int v : col) cnt2 += v % 2;
351+
for (int v : row) {
352+
cnt1 += v % 2;
353+
}
354+
for (int v : col) {
355+
cnt2 += v % 2;
356+
}
343357
return cnt1 * (n - cnt2) + cnt2 * (m - cnt1);
344358
}
345359
};

solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ The final matrix is [[1,3,1],[1,3,1]], which contains 6 odd numbers.
7171

7272
### Solution 1: Simulation
7373

74-
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.
74+
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.
7575

76-
After the simulation, we traverse the matrix and count the number of odd numbers.
76+
After the simulation ends, we traverse the matrix and count the number of odd numbers.
7777

78-
The time complexity is $O(\text{indices.length} \times (m+n) + mn)$, and the space complexity is $O(mn)$.
78+
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}$.
7979

8080
<!-- tabs:start -->
8181

@@ -128,12 +128,19 @@ public:
128128
vector<vector<int>> g(m, vector<int>(n));
129129
for (auto& e : indices) {
130130
int r = e[0], c = e[1];
131-
for (int i = 0; i < m; ++i) ++g[i][c];
132-
for (int j = 0; j < n; ++j) ++g[r][j];
131+
for (int i = 0; i < m; ++i) {
132+
++g[i][c];
133+
}
134+
for (int j = 0; j < n; ++j) {
135+
++g[r][j];
136+
}
133137
}
134138
int ans = 0;
135-
for (auto& row : g)
136-
for (int v : row) ans += v % 2;
139+
for (auto& row : g) {
140+
for (int v : row) {
141+
ans += v % 2;
142+
}
143+
}
137144
return ans;
138145
}
139146
};
@@ -174,11 +181,11 @@ func oddCells(m int, n int, indices [][]int) int {
174181

175182
### Solution 2: Space Optimization
176183

177-
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.
184+
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.
178185

179-
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.
186+
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.
180187

181-
The time complexity is $O(\text{indices.length} + mn)$, and the space complexity is $O(m+n)$.
188+
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}$.
182189

183190
<!-- tabs:start -->
184191

@@ -232,8 +239,11 @@ public:
232239
col[c]++;
233240
}
234241
int ans = 0;
235-
for (int i : row)
236-
for (int j : col) ans += (i + j) % 2;
242+
for (int i : row) {
243+
for (int j : col) {
244+
ans += (i + j) % 2;
245+
}
246+
}
237247
return ans;
238248
}
239249
};
@@ -268,11 +278,11 @@ func oddCells(m int, n int, indices [][]int) int {
268278

269279
### Solution 3: Mathematical Optimization
270280

271-
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.
281+
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.
272282

273-
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)$.
283+
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})$.
274284

275-
The time complexity is $O(\text{indices.length} + m + n)$, and the space complexity is $O(m+n)$.
285+
The time complexity is $O(k + m + n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\text{indices}$.
276286

277287
<!-- tabs:start -->
278288

@@ -329,8 +339,12 @@ public:
329339
col[c]++;
330340
}
331341
int cnt1 = 0, cnt2 = 0;
332-
for (int v : row) cnt1 += v % 2;
333-
for (int v : col) cnt2 += v % 2;
342+
for (int v : row) {
343+
cnt1 += v % 2;
344+
}
345+
for (int v : col) {
346+
cnt2 += v % 2;
347+
}
334348
return cnt1 * (n - cnt2) + cnt2 * (m - cnt1);
335349
}
336350
};

solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ class Solution {
44
vector<vector<int>> g(m, vector<int>(n));
55
for (auto& e : indices) {
66
int r = e[0], c = e[1];
7-
for (int i = 0; i < m; ++i) ++g[i][c];
8-
for (int j = 0; j < n; ++j) ++g[r][j];
7+
for (int i = 0; i < m; ++i) {
8+
++g[i][c];
9+
}
10+
for (int j = 0; j < n; ++j) {
11+
++g[r][j];
12+
}
913
}
1014
int ans = 0;
11-
for (auto& row : g)
12-
for (int v : row) ans += v % 2;
15+
for (auto& row : g) {
16+
for (int v : row) {
17+
ans += v % 2;
18+
}
19+
}
1320
return ans;
1421
}
1522
};

solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution2.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ class Solution {
99
col[c]++;
1010
}
1111
int ans = 0;
12-
for (int i : row)
13-
for (int j : col) ans += (i + j) % 2;
12+
for (int i : row) {
13+
for (int j : col) {
14+
ans += (i + j) % 2;
15+
}
16+
}
1417
return ans;
1518
}
1619
};

solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution3.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ class Solution {
99
col[c]++;
1010
}
1111
int cnt1 = 0, cnt2 = 0;
12-
for (int v : row) cnt1 += v % 2;
13-
for (int v : col) cnt2 += v % 2;
12+
for (int v : row) {
13+
cnt1 += v % 2;
14+
}
15+
for (int v : col) {
16+
cnt2 += v % 2;
17+
}
1418
return cnt1 * (n - cnt2) + cnt2 * (m - cnt1);
1519
}
1620
};

0 commit comments

Comments
 (0)