Find cells with odd values in a matrix
0
/
359
- 1 <= n <= 50
- 1 <= m <= 50
- 1 <= indices.length <= 100
- 0 <= indices[i][0] < n
- 0 <= indices[i][1] < m
- Initialize the output matrix
- Loop through and extract the ith row and columns
- Increment the cells in the ith row and ith columns obtained from previous step by 1
- Check the matrix cells by looping through the entries, increment the counter if the entry is odd and return the count
/**
* @param {number} n
* @param {number} m
* @param {number[][]} indices
* @return {number}
*/
var oddCells = function(n, m, indices) {
var matrix = zeros([n,m]);
var count = 0;
for (var i = 0; i < indices.length; i++) {
var row = indices[i][0];
var col = indices[i][1];
matrix = incrementCells(n, m, row, col, matrix);
}
for (var i = 0; i < n; i++) {
for (var j = 0; j < m; j++) {
if (matrix[i][j] % 2 !== 0)
count++;
}
}
return count;
};
var incrementCells = function(n, m, row, col, matrix) {
for (var i = 0; i < m; i++) {
matrix[row][i]++;
}
for (var j = 0; j < n; j++) {
matrix[j][col]++;
}
return matrix;
}
var zeros = function(dimensions) {
var array = [];
for (var i = 0; i < dimensions[0]; ++i) {
array.push(dimensions.length == 1 ? 0 : zeros(dimensions.slice(1)));
}
return array;
}