531 Lonely Pixel I
Input:
[['W', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'W']]
Output:
3
Explanation:
All the three 'B's are black lonely pixels.Last updated
Input:
[['W', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'W']]
Output:
3
Explanation:
All the three 'B's are black lonely pixels.Last updated
int findLonelyPixel(vector<vector<char>>& picture) {
if (picture.empty()) return 0;
const size_t rows = picture.size();
const size_t cols = picture.at(0).size();
vector<int> rowsWithB(rows);
vector<int> colsWithB(cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (picture[i][j] == 'B') {
rowsWithB[i]++; colsWithB[j]++;
}
}
}
int count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (picture[i][j] == 'B' && rowsWithB[i] == 1 && colsWithB[j] == 1) {
count++;
}
}
}
return count;
}