Skip to content

Commit cf32f43

Browse files
add 533
1 parent 9ab120e commit cf32f43

File tree

3 files changed

+36
-58
lines changed

3 files changed

+36
-58
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Your ideas/fixes/algorithms are more than welcome!
5959
|537|[Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ComplexNumberMultiplication.java) | O(1) |O(1) | Medium | Math, String
6060
|536|[Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ConstructBinaryTreefromString.java) | O(n) |O(h) | Medium | Recursion
6161
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../master/src/main/java/com/fishercoder/solutions/EncodeandDecodeTinyURL.java) | O(1) |O(n) | Medium | Design
62+
|533|[Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_533.java) | O(m*n) |O(m) (m is number of rows) | Medium | HashMap
6263
|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/KdiffPairsinanArray.java) | O(n) |O(n) | Easy | HashMap
6364
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/LonelyPixelI.java) | O(m*n) |O(1) | Medium |
6465
|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MinimumAbsoluteDifferenceinBST.java) | O(n) |O(n) | Easy| DFS

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

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,69 +37,47 @@
3737
The range of width and height of the input 2D array is [1,200].
3838
*/
3939
public class _533 {
40-
40+
/**Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5
41+
*
42+
* This program is very well designed to do things:
43+
* 1. it scans the entire matrix once, but does two things in this scan:
44+
* first it records an array of cols that keeps count of how many 'B' are there in each column;
45+
* second, at the end of traversing each column, it puts this entire row as the key into a HashMap
46+
* when there N number of 'B's in this row.
47+
*
48+
* 2. then we could go through the HashMap keyset:
49+
* if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's*/
4150
public int findBlackPixel(char[][] picture, int N) {
42-
Set<Integer> rows = new HashSet<>();
43-
Set<Integer> cols = new HashSet<>();
51+
if (picture == null || picture.length == 0 || picture[0].length == 0) return 0;
4452
int m = picture.length;
4553
int n = picture[0].length;
54+
int[] cols = new int[n];
55+
Map<String, Integer> map = new HashMap<>();
56+
StringBuilder stringBuilder = new StringBuilder();
4657
for (int i = 0; i < m; i++) {
47-
int blackPixelRowCount = 0;
58+
int count = 0;
4859
for (int j = 0; j < n; j++) {
49-
if (picture[i][j] == 'B') blackPixelRowCount++;
50-
}
51-
if (blackPixelRowCount == N) rows.add(i);
52-
}
53-
54-
for (int j = 0; j < n; j++) {
55-
int blackPixelColCount = 0;
56-
for (int i = 0; i < m; i++) {
57-
if (picture[i][j] == 'B') blackPixelColCount++;
58-
}
59-
if (blackPixelColCount == N) cols.add(j);
60-
}
61-
62-
if (!cols.isEmpty()) {
63-
Iterator<Integer> colIterator = cols.iterator();
64-
int colNumber = colIterator.next();
65-
Iterator<Integer> rowIterator = rows.iterator();
66-
int compareRowNumber = -1;
67-
Set<Integer> rowsThatHaveBlackAtThisCol = new HashSet<>();
68-
boolean firstTime = true;
69-
while (rowIterator.hasNext()) {
70-
int rowNumber = rowIterator.next();
71-
if (picture[rowNumber][colNumber] == 'B' && firstTime) {
72-
compareRowNumber = rowNumber;
73-
firstTime = false;
74-
} else if (picture[rowNumber][colNumber] == 'B') {
75-
rowsThatHaveBlackAtThisCol.add(rowNumber);
60+
if (picture[i][j] == 'B') {
61+
count++;
62+
cols[j]++;
7663
}
64+
stringBuilder.append(picture[i][j]);
7765
}
78-
if (compareRowNumber == -1) return 0;
79-
List<Integer> candidateRows = new ArrayList<>();
80-
Iterator<Integer> iterator = rowsThatHaveBlackAtThisCol.iterator();
81-
while (iterator.hasNext()) {
82-
int row = iterator.next();
83-
if (Arrays.equals(picture[row], picture[compareRowNumber])){
84-
candidateRows.add(row);
85-
}
66+
if (count == N) {
67+
/**we use this entire row string as key for the map*/
68+
map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1);
8669
}
87-
if (candidateRows.size() == 0) return 0;
88-
89-
candidateRows.add(compareRowNumber);
70+
stringBuilder.setLength(0);
71+
}
9072

91-
colIterator = cols.iterator();
92-
int result = 0;
93-
for (int i = 0; i < candidateRows.size(); i++) {
94-
for (int j = 0; j < n; j++) {
95-
if (cols.contains(j) && picture[i][j] == 'B') {
96-
result++;
97-
}
98-
}
73+
int answer = 0;
74+
for (String key : map.keySet()) {
75+
if (map.get(key) != N) continue;
76+
for (int i = 0; i < n; i++) {
77+
if (key.charAt(i) == 'B' && cols[i] == N) answer += N;
9978
}
100-
return result;
10179
}
102-
return 0;
80+
return answer;
10381
}
10482

10583
}

src/test/java/com/fishercoder/_533Test.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fishercoder.solutions._533;
44
import org.junit.BeforeClass;
5-
import org.junit.Ignore;
65
import org.junit.Test;
76

87
import static org.junit.Assert.assertEquals;
@@ -29,9 +28,9 @@ public void test1(){
2928
assertEquals(6, test.findBlackPixel(picture, 3));
3029
}
3130

32-
// @Test
33-
// public void test2(){
34-
// picture = new char[][]{{'B'}};
35-
// assertEquals(1, test.findBlackPixel(picture, 1));
36-
// }
31+
@Test
32+
public void test2(){
33+
picture = new char[][]{{'B'}};
34+
assertEquals(1, test.findBlackPixel(picture, 1));
35+
}
3736
}

0 commit comments

Comments
 (0)