forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCells with Odd Values in a Matrix.java
51 lines (43 loc) · 1.29 KB
/
Cells with Odd Values in a Matrix.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
// --------------------- Solution 1 ---------------------
class Solution {
public int oddCells(int m, int n, int[][] indices) {
int[][] matrix = new int[m][n];
for(int i = 0; i < indices.length; i++) {
int row = indices[i][0];
int col = indices[i][1];
for(int j = 0; j < n; j++) {
matrix[row][j]++;
}
for(int j = 0; j < m; j++) {
matrix[j][col]++;
}
}
int counter = 0;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(matrix[i][j] % 2 != 0) {
counter++;
}
}
}
return counter;
}
}
// --------------------- Solution 2 ---------------------
class Solution {
public int oddCells(int m, int n, int[][] indices) {
int[] row = new int[m];
int[] col = new int[n];
for(int i = 0; i < indices.length; i++) {
row[indices[i][0]]++;
col[indices[i][1]]++;
}
int counter = 0;
for(int i : row) {
for(int j : col) {
counter += (i + j) % 2 == 0 ? 0 : 1;
}
}
return counter;
}
}