Cells with Odd Values in a Matrix LeetCode solution in Java

1
// --------------------- Solution 1 ---------------------
2
class Solution {
3
public int oddCells(int m, int n, int[][] indices) {
4
int[][] matrix = new int[m][n];
5

6
for (int i = 0; i < indices.length; i++) {
7
int row = indices[i][0];
8
int col = indices[i][1];
9

10
for (int j = 0; j < n; j++) {
11
matrix[row][j]++;
12
}
13
for (int j = 0; j < m; j++) {
14
matrix[j][col]++;
15
}
16
}
17

18
int counter = 0;
19
for (int i = 0; i < m; i++) {
20
for (int j = 0; j < n; j++) {
21
if (matrix[i][j] % 2 != 0) {
22
counter++;
23
}
24
}
25
}
26

27
return counter;
28
}
29
}
30

31
// --------------------- Solution 2 ---------------------
32
class Solution {
33
public int oddCells(int m, int n, int[][] indices) {
34
int[] row = new int[m];
35
int[] col = new int[n];
36

37
for (int i = 0; i < indices.length; i++) {
38
row[indices[i][0]]++;
39
col[indices[i][1]]++;
40
}
41

42
int counter = 0;
43
for (int i : row) {
44
for (int j : col) {
45
counter += (i + j) % 2 == 0 ? 0 : 1;
46
}
47
}
48

49
return counter;
50
}
51
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0