[LeetCode] Lonely Pixel I

531. Lonely Pixel I

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& A) {
int n = A.size(), m = A[0].size(), res = 0;
vector<int> row(n), col(m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(A[i][j] == 'B') {
row[i]++;
col[j]++;
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(A[i][j] == 'B' and row[i] == 1 and col[j] == 1) res++;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/04/PS/LeetCode/lonely-pixel-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.