Skip to content

Commit d159d7d

Browse files
refactor 533
1 parent 0d19238 commit d159d7d

File tree

2 files changed

+47
-48
lines changed

2 files changed

+47
-48
lines changed

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

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,55 @@
3838
The range of width and height of the input 2D array is [1,200].
3939
*/
4040
public class _533 {
41-
/**Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5
42-
*
43-
* This program is very well designed to do things:
44-
* 1. it scans the entire matrix once, but does two things in this scan:
45-
* first it records an array of cols that keeps count of how many 'B' are there in each column;
46-
* second, at the end of traversing each column, it puts this entire row as the key into a HashMap
47-
* when there N number of 'B's in this row.
48-
*
49-
* 2. then we could go through the HashMap keyset:
50-
* 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*/
51-
public int findBlackPixel(char[][] picture, int N) {
52-
if (picture == null || picture.length == 0 || picture[0].length == 0) {
53-
return 0;
54-
}
55-
int m = picture.length;
56-
int n = picture[0].length;
57-
int[] cols = new int[n];
58-
Map<String, Integer> map = new HashMap<>();
59-
StringBuilder stringBuilder = new StringBuilder();
60-
for (int i = 0; i < m; i++) {
61-
int count = 0;
62-
for (int j = 0; j < n; j++) {
63-
if (picture[i][j] == 'B') {
64-
count++;
65-
cols[j]++;
66-
}
67-
stringBuilder.append(picture[i][j]);
41+
public static class Solution1 {
42+
/**
43+
* Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5
44+
* This program is very well designed to do things:
45+
* 1. it scans the entire matrix once, but does two things in this scan:
46+
* first it records an array of cols that keeps count of how many 'B' are there in each column;
47+
* second, at the end of traversing each column, it puts this entire row as the key into a HashMap
48+
* when there N number of 'B's in this row.
49+
* 2. then we could go through the HashMap keyset:
50+
* 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
51+
*/
52+
public int findBlackPixel(char[][] picture, int N) {
53+
if (picture == null || picture.length == 0 || picture[0].length == 0) {
54+
return 0;
6855
}
69-
if (count == N) {
70-
/**we use this entire row string as key for the map*/
71-
map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1);
56+
int m = picture.length;
57+
int n = picture[0].length;
58+
int[] cols = new int[n];
59+
Map<String, Integer> map = new HashMap<>();
60+
StringBuilder stringBuilder = new StringBuilder();
61+
for (int i = 0; i < m; i++) {
62+
int count = 0;
63+
for (int j = 0; j < n; j++) {
64+
if (picture[i][j] == 'B') {
65+
count++;
66+
cols[j]++;
67+
}
68+
stringBuilder.append(picture[i][j]);
69+
}
70+
if (count == N) {
71+
/**we use this entire row string as key for the map*/
72+
map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1);
73+
}
74+
stringBuilder.setLength(0);
7275
}
73-
stringBuilder.setLength(0);
74-
}
7576

76-
int answer = 0;
77-
for (String key : map.keySet()) {
78-
if (map.get(key) != N) {
79-
continue;
80-
}
81-
for (int i = 0; i < n; i++) {
82-
if (key.charAt(i) == 'B' && cols[i] == N) {
83-
answer += N;
77+
int answer = 0;
78+
for (String key : map.keySet()) {
79+
if (map.get(key) != N) {
80+
continue;
81+
}
82+
for (int i = 0; i < n; i++) {
83+
if (key.charAt(i) == 'B' && cols[i] == N) {
84+
answer += N;
85+
}
8486
}
8587
}
88+
return answer;
8689
}
87-
return answer;
8890
}
8991

9092
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/25/17.
11-
*/
129
public class _533Test {
13-
private static _533 test;
10+
private static _533.Solution1 solution1;
1411
private static char[][] picture;
1512

1613
@BeforeClass
1714
public static void setup() {
18-
test = new _533();
15+
solution1 = new _533.Solution1();
1916
}
2017

2118
@Test
@@ -25,12 +22,12 @@ public void test1() {
2522
{'W', 'B', 'W', 'B', 'B', 'W'},
2623
{'W', 'B', 'W', 'B', 'B', 'W'},
2724
{'W', 'W', 'B', 'W', 'B', 'W'}};
28-
assertEquals(6, test.findBlackPixel(picture, 3));
25+
assertEquals(6, solution1.findBlackPixel(picture, 3));
2926
}
3027

3128
@Test
3229
public void test2() {
3330
picture = new char[][]{{'B'}};
34-
assertEquals(1, test.findBlackPixel(picture, 1));
31+
assertEquals(1, solution1.findBlackPixel(picture, 1));
3532
}
3633
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy