- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1252.java
60 lines (56 loc) · 1.9 KB
/
_1252.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
packagecom.fishercoder.solutions.secondthousand;
publicclass_1252 {
publicstaticclassSolution1 {
/*
* Time: O(m*n + k) where k is the length of indices
* Space: O(m*n)
*/
publicintoddCells(intn, intm, int[][] indices) {
int[][] matrix = newint[n][m];
for (inti = 0; i < indices.length; i++) {
addOneToRow(matrix, indices[i][0]);
addOneToColumn(matrix, indices[i][1]);
}
intoddNumberCount = 0;
for (inti = 0; i < matrix.length; i++) {
for (intj = 0; j < matrix[0].length; j++) {
if (matrix[i][j] % 2 != 0) {
oddNumberCount++;
}
}
}
returnoddNumberCount;
}
privatevoidaddOneToColumn(int[][] matrix, intcolumnIndex) {
for (inti = 0; i < matrix.length; i++) {
matrix[i][columnIndex] += 1;
}
}
privatevoidaddOneToRow(int[][] matrix, introwIndex) {
for (intj = 0; j < matrix[0].length; j++) {
matrix[rowIndex][j] += 1;
}
}
}
publicstaticclassSolution2 {
/*
* Time: O(m*n + k) where k is the length of indices
* Space: O(m + n)
*/
publicintoddCells(intn, intm, int[][] indices) {
boolean[] row = newboolean[n];
boolean[] column = newboolean[m];
for (int[] index : indices) {
row[index[0]] ^= true;
column[index[1]] ^= true;
}
intoddNumberCount = 0;
for (inti = 0; i < n; i++) {
for (intj = 0; j < m; j++) {
oddNumberCount += row[i] ^ column[j] ? 1 : 0;
}
}
returnoddNumberCount;
}
}
}