Skip to content

Commit 6d4a8d4

Browse files
solves cells with odd values in matrix
1 parent 056bc56 commit 6d4a8d4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@
324324
| 1228 | 🔒 [Missing A Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression) | | |
325325
| 1232 | [Check If It Is A Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line) | [![Java](assets/java.png)](src/CheckIfItIsASStraightLine.java) | |
326326
| 1237 | [Find Positive Integer Solutions for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation) | | |
327-
| 1243 | [Array Transformation](https://leetcode.com/problems/array-transformation) | | |
328-
| 1252 | [Cells With Odd Values In Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix) | | |
327+
| 1243 | 🔒 [Array Transformation](https://leetcode.com/problems/array-transformation) | | |
328+
| 1252 | [Cells With Odd Values In Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix) | [![Java](assets/java.png)](src/CellsWithOddValuesInMatrix.java) | |
329329
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid) | | |
330330
| 1266 | [Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points) | | |
331331
| 1271 | [Hexspeak](https://leetcode.com/problems/hexspeak) | | |

src/CellsWithOddValuesInMatrix.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class CellsWithOddValuesInMatrix {
2+
public int oddCells(int m, int n, int[][] operations) {
3+
final byte[] rowState = new byte[m];
4+
final byte[] columnState = new byte[n];
5+
for (int[] operation : operations) {
6+
rowState[operation[0]] ^= 1;
7+
columnState[operation[1]] ^= 1;
8+
}
9+
int oddElements = 0;
10+
final int shiftedColumns = sum(columnState);
11+
for (int row : rowState) {
12+
oddElements += row == 0 ? shiftedColumns : n - shiftedColumns;
13+
}
14+
return oddElements;
15+
}
16+
17+
private int sum(byte[] array) {
18+
int sum = 0;
19+
for (byte element : array) sum += element;
20+
return sum;
21+
}
22+
}

0 commit comments

Comments
 (0)