Skip to content

Commit b9ef46e

Browse files
committed
feat: add solutions to lc problem: No.1252
No.1252.Cells with Odd Values in a Matrix
1 parent 1485fdc commit b9ef46e

File tree

6 files changed

+358
-59
lines changed

6 files changed

+358
-59
lines changed

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

Lines changed: 179 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:模拟**
66+
67+
创建一个矩阵 $g$ 来存放操作的结果。对于 $indices$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。
68+
69+
模拟结束后,遍历矩阵,统计奇数的个数。
70+
71+
时间复杂度 $O(indices.length*(m+n)+mn)$,空间复杂度 $O(mn)$。
72+
73+
**方法二:空间优化**
74+
75+
用行数组 $row$ 和列数组 $col$ 来记录每一行、每一列被增加的次数。对于 $indices$ 中的每一对 $(r_i, c_i)$,我们将 $row[r_i]$ 和 $col[c_i]$ 分别加 $1$。
76+
77+
操作结束后,可以算出 $(i, j)$ 位置的计数为 $row[i]+col[j]$。遍历矩阵,统计奇数的个数。
78+
79+
时间复杂度 $O(indices.length+mn)$,空间复杂度 $O(m+n)$。
80+
81+
**方法三:数学优化**
82+
83+
我们注意到,只有当 $row[i]$ 和 $col[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。
84+
85+
我们统计 $row$ 中的奇数个数,记为 $cnt1$;$col$ 中的奇数个数,记为 $cnt2$。那么最终得到的奇数个数为 $cnt1*(n-cnt2)+cnt2*(m-cnt1)$。
86+
87+
时间复杂度 $O(indices.length+m+n)$,空间复杂度 $O(m+n)$。
88+
6589
<!-- tabs:start -->
6690

6791
### **Python3**
@@ -77,7 +101,31 @@ class Solution:
77101
g[i][c] += 1
78102
for j in range(n):
79103
g[r][j] += 1
80-
return sum(g[i][j] % 2 for i in range(m) for j in range(n))
104+
return sum(v % 2 for row in g for v in row)
105+
```
106+
107+
```python
108+
class Solution:
109+
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
110+
row = [0] * m
111+
col = [0] * n
112+
for r, c in indices:
113+
row[r] += 1
114+
col[c] += 1
115+
return sum((i + j) % 2 for i in row for j in col)
116+
```
117+
118+
```python
119+
class Solution:
120+
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
121+
row = [0] * m
122+
col = [0] * n
123+
for r, c in indices:
124+
row[r] += 1
125+
col[c] += 1
126+
cnt1 = sum(v % 2 for v in row)
127+
cnt2 = sum(v % 2 for v in col)
128+
return cnt1 * (n - cnt2) + cnt2 * (m - cnt1)
81129
```
82130

83131
### **Java**
@@ -91,23 +139,66 @@ class Solution {
91139
for (int[] e : indices) {
92140
int r = e[0], c = e[1];
93141
for (int i = 0; i < m; ++i) {
94-
++g[i][c];
142+
g[i][c]++;
95143
}
96144
for (int j = 0; j < n; ++j) {
97-
++g[r][j];
145+
g[r][j]++;
98146
}
99147
}
100148
int ans = 0;
101-
for (int i = 0; i < m; ++i) {
102-
for (int j = 0; j < n; ++j) {
103-
ans += g[i][j] % 2;
149+
for (int[] row : g) {
150+
for (int v : row) {
151+
ans += v % 2;
104152
}
105153
}
106154
return ans;
107155
}
108156
}
109157
```
110158

159+
```java
160+
class Solution {
161+
public int oddCells(int m, int n, int[][] indices) {
162+
int[] row = new int[m];
163+
int[] col = new int[n];
164+
for (int[] e : indices) {
165+
int r = e[0], c = e[1];
166+
row[r]++;
167+
col[c]++;
168+
}
169+
int ans = 0;
170+
for (int i : row) {
171+
for (int j : col) {
172+
ans += (i + j) % 2;
173+
}
174+
}
175+
return ans;
176+
}
177+
}
178+
```
179+
180+
```java
181+
class Solution {
182+
public int oddCells(int m, int n, int[][] indices) {
183+
int[] row = new int[m];
184+
int[] col = new int[n];
185+
for (int[] e : indices) {
186+
int r = e[0], c = e[1];
187+
row[r]++;
188+
col[c]++;
189+
}
190+
int cnt1 = 0, cnt2 = 0;
191+
for (int v : row) {
192+
cnt1 += v % 2;
193+
}
194+
for (int v : col) {
195+
cnt2 += v % 2;
196+
}
197+
return cnt1 * (n - cnt2) + cnt2 * (m - cnt1);
198+
}
199+
}
200+
```
201+
111202
### **C++**
112203

113204
```cpp
@@ -122,14 +213,51 @@ public:
122213
for (int j = 0; j < n; ++j) ++g[r][j];
123214
}
124215
int ans = 0;
125-
for (int i = 0; i < m; ++i)
126-
for (int j = 0; j < n; ++j)
127-
ans += g[i][j] % 2;
216+
for (auto& row : g) for (int v : row) ans += v % 2;
128217
return ans;
129218
}
130219
};
131220
```
132221
222+
```cpp
223+
class Solution {
224+
public:
225+
int oddCells(int m, int n, vector<vector<int>>& indices) {
226+
vector<int> row(m);
227+
vector<int> col(n);
228+
for (auto& e : indices)
229+
{
230+
int r = e[0], c = e[1];
231+
row[r]++;
232+
col[c]++;
233+
}
234+
int ans = 0;
235+
for (int i : row) for (int j : col) ans += (i + j) % 2;
236+
return ans;
237+
}
238+
};
239+
```
240+
241+
```cpp
242+
class Solution {
243+
public:
244+
int oddCells(int m, int n, vector<vector<int>>& indices) {
245+
vector<int> row(m);
246+
vector<int> col(n);
247+
for (auto& e : indices)
248+
{
249+
int r = e[0], c = e[1];
250+
row[r]++;
251+
col[c]++;
252+
}
253+
int cnt1 = 0, cnt2 = 0;
254+
for (int v : row) cnt1 += v % 2;
255+
for (int v : col) cnt2 += v % 2;
256+
return cnt1 * (n - cnt2) + cnt2 * (m - cnt1);
257+
}
258+
};
259+
```
260+
133261
### **Go**
134262
135263
```go
@@ -148,15 +276,54 @@ func oddCells(m int, n int, indices [][]int) int {
148276
}
149277
}
150278
ans := 0
151-
for i := 0; i < m; i++ {
152-
for j := 0; j < n; j++ {
153-
ans += g[i][j] % 2
279+
for _, row := range g {
280+
for _, v := range row {
281+
ans += v % 2
154282
}
155283
}
156284
return ans
157285
}
158286
```
159287

288+
```go
289+
func oddCells(m int, n int, indices [][]int) int {
290+
row := make([]int, m)
291+
col := make([]int, n)
292+
for _, e := range indices {
293+
r, c := e[0], e[1]
294+
row[r]++
295+
col[c]++
296+
}
297+
ans := 0
298+
for _, i := range row {
299+
for _, j := range col {
300+
ans += (i + j) % 2
301+
}
302+
}
303+
return ans
304+
}
305+
```
306+
307+
```go
308+
func oddCells(m int, n int, indices [][]int) int {
309+
row := make([]int, m)
310+
col := make([]int, n)
311+
for _, e := range indices {
312+
r, c := e[0], e[1]
313+
row[r]++
314+
col[c]++
315+
}
316+
cnt1, cnt2 := 0, 0
317+
for _, v := range row {
318+
cnt1 += v % 2
319+
}
320+
for _, v := range col {
321+
cnt2 += v % 2
322+
}
323+
return cnt1*(n-cnt2) + cnt2*(m-cnt1)
324+
}
325+
```
326+
160327
### **...**
161328

162329
```

0 commit comments

Comments
 (0)