|
37 | 37 | The range of width and height of the input 2D array is [1,200]. |
38 | 38 | */ |
39 | 39 | 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*/ |
41 | 50 | 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; |
44 | 52 | int m = picture.length; |
45 | 53 | int n = picture[0].length; |
| 54 | + int[] cols = new int[n]; |
| 55 | + Map<String, Integer> map = new HashMap<>(); |
| 56 | + StringBuilder stringBuilder = new StringBuilder(); |
46 | 57 | for (int i = 0; i < m; i++) { |
47 | | - int blackPixelRowCount = 0; |
| 58 | + int count = 0; |
48 | 59 | 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]++; |
76 | 63 | } |
| 64 | + stringBuilder.append(picture[i][j]); |
77 | 65 | } |
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); |
86 | 69 | } |
87 | | - if (candidateRows.size() == 0) return 0; |
88 | | - |
89 | | - candidateRows.add(compareRowNumber); |
| 70 | + stringBuilder.setLength(0); |
| 71 | + } |
90 | 72 |
|
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; |
99 | 78 | } |
100 | | - return result; |
101 | 79 | } |
102 | | - return 0; |
| 80 | + return answer; |
103 | 81 | } |
104 | 82 |
|
105 | 83 | } |
0 commit comments