#531
Medium Algorithms Lonely pixel i
Array Hash Table Matrix
62.7% acceptance
Mar 31, 2026
451
41
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.
Solution
Rust
Time O(n²)
Space O(n)
impl Solution {
pub fn find_lonely_pixel(picture: Vec<Vec<char>>) -> i32 {
let m = picture.len();
let n = picture[0].len();
let mut row_count = vec![0; m];
let mut col_count = vec![0; n];
for i in 0..m {
for j in 0..n {
if picture[i][j] == 'B' {
row_count[i] += 1;
col_count[j] += 1;
}
}
}
let mut count = 0;
for i in 0..m {
for j in 0..n {
if picture[i][j] == 'B' && row_count[i] == 1 && col_count[j] == 1 {
count += 1;
}
}
}
count
}
}