Skip to content

Commit 84673bb

Browse files
authored
Create 1252-cells-with-odd-values-in-a-matrix.js
1 parent 1c09df7 commit 84673bb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} m
4+
* @param {number[][]} indices
5+
* @return {number}
6+
*/
7+
const oddCells = function (n, m, indices) {
8+
const oddRows = new BitSet(n),
9+
oddCols = new BitSet(m)
10+
let cntRow = 0,
11+
cntCol = 0
12+
for (let idx of indices) {
13+
oddRows.flip(idx[0])
14+
oddCols.flip(idx[1])
15+
cntRow += oddRows.get(idx[0]) ? 1 : -1
16+
cntCol += oddCols.get(idx[1]) ? 1 : -1
17+
}
18+
return (m - cntCol) * cntRow + (n - cntRow) * cntCol
19+
}
20+
21+
class BitSet {
22+
constructor(n) {
23+
this.arr = Array(n).fill(0)
24+
}
25+
flip(idx) {
26+
this.arr[idx] = this.arr[idx] === 0 ? 1 : 0
27+
}
28+
get(idx) {
29+
return this.arr[idx]
30+
}
31+
}

0 commit comments

Comments
 (0)