-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCells_1252.java
More file actions
33 lines (30 loc) · 1.03 KB
/
Cells_1252.java
File metadata and controls
33 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Cells_1252 {
// Time: O(L), space: O(m + n), where L = indices.length.
public int oddCells(int n, int m, int[][] indices) {
boolean[] row = new boolean[n], col = new boolean[m];
int cntRow = 0, cntCol = 0;
for (int[] idx : indices) {
row[idx[0]] ^= true;
col[idx[1]] ^= true;
cntRow += row[idx[0]] ? 1 : -1;
cntCol += col[idx[1]] ? 1 : -1;
}
return m * cntRow + n * cntCol - 2 * cntRow * cntCol;
}
// Time: O(L + m + n), space: O(m + n), where L = indices.length
/*
public int oddCells(int n, int m, int[][] indices) {
boolean[] row = new boolean[n], col = new boolean[m];
int cntRow = 0, cntCol = 0;
for (int[] idx : indices) {
row[idx[0]] ^= true;
col[idx[1]] ^= true;
}
for (boolean r : row)
cntRow += r ? 1 : 0;
for (boolean c : col)
cntCol += c ? 1 : 0;
return m * cntRow + n * cntCol - 2 * cntRow * cntCol;
}
*/
}