Skip to content

Commit 2c048d8

Browse files
committedJan 5, 2020
Add solution #1252
1 parent 6bd3118 commit 2c048d8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1252. Cells with Odd Values in a Matrix
3+
* https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/
4+
* Difficulty: Easy
5+
*
6+
* Given `n` and `m` which are the dimensions of a matrix
7+
* initialized by zeros and given an array `indices`
8+
* where `indices[i] = [ri, ci]`.
9+
* For each pair of `[ri, ci]` you have to increment all
10+
* cells in row `ri` and column `ci` by 1.
11+
*
12+
* Return the number of cells with odd values in the
13+
* matrix after applying the increment to all `indices`.
14+
*/
15+
16+
/**
17+
* @param {number} n
18+
* @param {number} m
19+
* @param {number[][]} indices
20+
* @return {number}
21+
*/
22+
var oddCells = function(n, m, indices) {
23+
const matrix = new Array(n).fill().map(_ => new Array(m).fill(0));
24+
25+
indices.forEach(indice => {
26+
const [row, column] = indice;
27+
matrix[row].forEach((value, index) => matrix[row][index] = value + 1);
28+
matrix.forEach(row => row[column] = row[column] + 1);
29+
});
30+
31+
return matrix.reduce((count, row) => {
32+
return count + row.filter(value => value % 2 !== 0).length;
33+
}, 0);
34+
};

0 commit comments

Comments
 (0)
Please sign in to comment.