Skip to content

1252. Cells with Odd Values in a Matrix 👎

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int oddCells(int n, int m, vector<vector<int>>& indices) {
    int ans = 0;
    vector<int> rows(n);
    vector<int> cols(m);

    for (vector<int>& indice : indices) {
      rows[indice[0]] ^= 1;
      cols[indice[1]] ^= 1;
    }

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        ans += rows[i] ^ cols[j];

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
  def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int:
    ans = 0
    rows = [0] * n
    cols = [0] * m

    for indice in indices:
      rows[indice[0]] ^= 1
      cols[indice[1]] ^= 1

    for i in range(n):
      for j in range(m):
        ans += rows[i] ^ cols[j]

    return ans