Cells with Odd Values in a Matrix LeetCode solution in C++

1
static int x = []() {
2
std::ios::sync_with_stdio(false);
3
cin.tie(nullptr);
4
return 0;
5
}();
6

7
class Solution { // tc: O(n+m) & sc: O(n+m)
8
public:
9
int oddCells(int n, int m, vector<vector<int>> &indices) {
10
vector<bool> rows(n, false), cols(m, false);
11
for (auto index : indices) {
12
rows[index[0]] = rows[index[0]] ^ true;
13
cols[index[1]] = cols[index[1]] ^ true;
14
}
15

16
int r(0), c(0);
17
for (int i(0); i < n; i++) {
18
if (rows[i]) r++;
19
}
20

21
for (int i(0); i < m; i++) {
22
if (cols[i]) c++;
23
}
24
return r * (m - c) + c * (n - r); // (or) return (r*m + c*n - 2*r*c);
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0