Cells with Odd Values in a Matrix LeetCode solution in JavaScript
1var oddCells = function (m, n, indices) {2const matrix = Array.from(Array(m), () => Array(n).fill(0));34let res = 0;5for (const [r, c] of indices) {6for (let i = 0; i < n; i++) {7// toggle 0/1 for even/odd8// another method: matrix[r][i] = 1 - matrix[r][i]9// or: matrix[r][i] = +!matrix[r][i]10matrix[r][i] ^= 1;11if (matrix[r][i]) res++;12else res--;13}1415for (let i = 0; i < m; i++) {16matrix[i][c] ^= 1;17if (matrix[i][c]) res++;18else res--;19}20}2122return res;23};