File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments