Given a picture consisting of black and white pixels, and a positive integer N, find the number of black pixels located at some specific row R and column C that align with all the following rules:
Row R and column C both contain exactly N black pixels.
For all rows that have a black pixel at column C, they should be exactly the same as row R
The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.
Example:
Input:
[['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'W', 'B', 'W', 'B', 'W']]
N = 3
Output: 6
Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3).
0 1 2 3 4 5 column index
0 [['W', 'B', 'W', 'B', 'B', 'W'],
1 ['W', 'B', 'W', 'B', 'B', 'W'],
2 ['W', 'B', 'W', 'B', 'B', 'W'],
3 ['W', 'W', 'B', 'W', 'B', 'W']]
row index
Take 'B' at row R = 0 and column C = 1 as an example:
Rule 1, row R = 0 and column C = 1 both have exactly N = 3 black pixels.
Rule 2, the rows have black pixel at column C = 1 are row 0, row 1 and row 2. They are exactly the same as row R = 0.
Note:
The range of width and height of the input 2D array is [1,200].
Solution:
思路:
給了一個(gè)整數(shù)N,說(shuō)對(duì)于均含有N個(gè)黑像素的某行某列讲冠,如果該列中所有的黑像素所在的行都相同的話椅寺,該列的所有黑像素均為孤獨(dú)的像素浆西,讓我們統(tǒng)計(jì)所有的這樣的孤獨(dú)的像素的個(gè)數(shù)歧蕉。
那么跟之前那題類似汰蓉,我們還是要統(tǒng)計(jì)每一行每一列的黑像素的個(gè)數(shù),而且由于條件二中要比較各行之間是否相等奋姿,如果一個(gè)字符一個(gè)字符的比較寫(xiě)起來(lái)比較麻煩锄开,我們可以用個(gè)trick,把每行的字符連起來(lái)称诗,形成一個(gè)字符串萍悴,然后直接比較兩個(gè)字符串是否相等會(huì)簡(jiǎn)單很多(用hashmap(string, count))。然后我們遍歷每一行和每一列寓免,如果某行和某列的黑像素剛好均為N癣诱,我們遍歷該列的所有黑像素,如果其所在行均相等袜香,則說(shuō)明該列的所有黑像素均為孤獨(dú)的像素撕予,將個(gè)數(shù)加入結(jié)果res中,然后將該行的黑像素統(tǒng)計(jì)個(gè)數(shù)清零蜈首,以免重復(fù)運(yùn)算实抡,這樣我們就可以求出所有的孤獨(dú)的像素了,
Time Complexity: O(N) Space Complexity: O(N)
Solution Code:
// 孤獨(dú)的點(diǎn)要滿足:
// (1) 該行有N個(gè)黑點(diǎn)
// (2) 該列有N個(gè)黑點(diǎn)
// (3) 該列中所有的黑像素所在的行都相同
public class Solution {
public int findBlackPixel(char[][] picture, int N) {
if(picture == null || picture.length == 0 || picture[0].length == 0) return 0;
int m = picture.length;
int n = picture[0].length;
Map<String, Integer> map = new HashMap<>();
int[] colCount = new int[n];
// (預(yù)備3)行編碼存入map欢策,(預(yù)備2)統(tǒng)計(jì)列的B吆寨,并check(1)該行B的個(gè)數(shù)是否為N
for (int i = 0; i < m; i++) {
String key = scanRow(picture, i, N, colCount);
if (key.length() != 0) {
map.put(key, map.getOrDefault(key, 0) + 1);
}
}
// check(3) 及 check(2)該列B的個(gè)數(shù)是否為N
int result = 0;
for (String key : map.keySet()) {
if (map.get(key) == N) {
for (int j = 0; j < n; j++) {
if (key.charAt(j) == 'B' && colCount[j] == N) {
result += N;
}
}
}
}
return result;
}
private String scanRow(char[][] picture, int row, int target, int[] colCount) {
int n = picture[0].length;
int rowCount = 0;
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
if (picture[row][j] == 'B') {
rowCount++;
colCount[j]++;
}
sb.append(picture[row][j]);
}
if (rowCount == target) return sb.toString();
return "";
}
}