Given an m x n
picture
consisting of black 'B'
and white 'W'
pixels, return the number of black lonely pixels.
A black lonely pixel is a character 'B'
that located at a specific position where the same row and same column don't have any other black pixels.
Example 1:
Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]] Output: 3 Explanation: All the three 'B's are black lonely pixels.
Example 2:
Input: picture = [["B","B","B"],["B","B","W"],["B","B","B"]] Output: 0
Constraints:
m == picture.length
n == picture[i].length
1 <= m, n <= 500
picture[i][j]
is'W'
or'B'
.
Related Topics:
Array, Hash Table, Matrix
Similar Questions:
// OJ: https://leetcode.com/problems/lonely-pixel-i
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(M + N)
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& A) {
int M = A.size(), N = A[0].size(), ans = 0;
vector<int> row(N), col(N);
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (A[i][j] == 'W') continue;
row[i]++;
col[j]++;
}
}
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (A[i][j] == 'W') continue;
if (row[i] == 1 && col[j] == 1) ++ans;
}
}
return ans;
}
};