Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fa9208b

Browse files
add one more solution for 1252
1 parent e6e57b0 commit fa9208b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30-
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n) | O(1) | |Easy||
30+
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||
3131
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | |Easy||
3232
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
3333
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||

src/main/java/com/fishercoder/solutions/_1252.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
* */
3535
public class _1252 {
3636
public static class Solution1 {
37+
/**
38+
* Time: O(m*n + k) where k is the length of indices
39+
* Space: O(m*n)
40+
* */
3741
public int oddCells(int n, int m, int[][] indices) {
3842
int[][] matrix = new int[n][m];
3943
for (int i = 0; i < indices.length; i++) {
@@ -63,4 +67,26 @@ private void addOneToRow(int[][] matrix, int rowIndex) {
6367
}
6468
}
6569
}
70+
71+
public static class Solution2 {
72+
/**
73+
* Time: O(m*n + k) where k is the length of indices
74+
* Space: O(m + n)
75+
*/
76+
public int oddCells(int n, int m, int[][] indices) {
77+
boolean[] row = new boolean[n];
78+
boolean[] column = new boolean[m];
79+
for (int[] index : indices) {
80+
row[index[0]] ^= true;
81+
column[index[1]] ^= true;
82+
}
83+
int oddNumberCount = 0;
84+
for (int i = 0; i < n; i++) {
85+
for (int j = 0; j < m; j++) {
86+
oddNumberCount += row[i] ^ column[j] ? 1 : 0;
87+
}
88+
}
89+
return oddNumberCount;
90+
}
91+
}
6692
}

src/test/java/com/fishercoder/_1252Test.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
public class _1252Test {
1010
private static _1252.Solution1 solution1;
11+
private static _1252.Solution2 solution2;
1112
private static int[][] indices;
1213

1314
@BeforeClass
1415
public static void setup() {
1516
solution1 = new _1252.Solution1();
17+
solution2 = new _1252.Solution2();
1618
}
1719

1820
@Test
@@ -33,4 +35,22 @@ public void test2() {
3335
assertEquals(0, solution1.oddCells(2, 2, indices));
3436
}
3537

38+
@Test
39+
public void test3() {
40+
indices = new int[][]{
41+
{0, 1},
42+
{1, 1}
43+
};
44+
assertEquals(6, solution2.oddCells(2, 3, indices));
45+
}
46+
47+
@Test
48+
public void test4() {
49+
indices = new int[][]{
50+
{1, 1},
51+
{0, 0}
52+
};
53+
assertEquals(0, solution2.oddCells(2, 2, indices));
54+
}
55+
3656
}

0 commit comments

Comments
 (0)