Lonely Pixel I
MediumUpdated: Aug 2, 2025
Practice on:
Problem
Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number ofblack 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.
Examples
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.lengthn == picture[i].length1 <= m, n <= 500picture[i][j]is'W'or'B'.
Solution
Method 1 – Row and Column Counting (1)
Intuition
A black pixel is lonely if it is the only 'B' in its row and column. By counting the number of 'B's in each row and column, we can efficiently check for lonely pixels.
Approach
- Initialize two arrays to count 'B's in each row and column.
- Traverse the matrix and increment the counts for each 'B'.
- Traverse the matrix again, and for each 'B', check if its row and column counts are both 1.
- Count such pixels and return the result.
Code
C++
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& pic) {
int m = pic.size(), n = pic[0].size();
vector<int> row(m), col(n);
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (pic[i][j] == 'B') row[i]++, col[j]++;
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (pic[i][j] == 'B' && row[i] == 1 && col[j] == 1) ans++;
return ans;
}
};
Go
func findLonelyPixel(pic [][]byte) int {
m, n := len(pic), len(pic[0])
row := make([]int, m)
col := make([]int, n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if pic[i][j] == 'B' {
row[i]++
col[j]++
}
}
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if pic[i][j] == 'B' && row[i] == 1 && col[j] == 1 {
ans++
}
}
}
return ans
}
Java
class Solution {
public int findLonelyPixel(char[][] pic) {
int m = pic.length, n = pic[0].length;
int[] row = new int[m], col = new int[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (pic[i][j] == 'B') { row[i]++; col[j]++; }
int ans = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (pic[i][j] == 'B' && row[i] == 1 && col[j] == 1) ans++;
return ans;
}
}
Kotlin
class Solution {
fun findLonelyPixel(pic: Array<CharArray>): Int {
val m = pic.size
val n = pic[0].size
val row = IntArray(m)
val col = IntArray(n)
for (i in 0 until m)
for (j in 0 until n)
if (pic[i][j] == 'B') { row[i]++; col[j]++ }
var ans = 0
for (i in 0 until m)
for (j in 0 until n)
if (pic[i][j] == 'B' && row[i] == 1 && col[j] == 1) ans++
return ans
}
}
Python
class Solution:
def findLonelyPixel(self, pic: list[list[str]]) -> int:
m, n = len(pic), len(pic[0])
row = [0] * m
col = [0] * n
for i in range(m):
for j in range(n):
if pic[i][j] == 'B':
row[i] += 1
col[j] += 1
ans = 0
for i in range(m):
for j in range(n):
if pic[i][j] == 'B' and row[i] == 1 and col[j] == 1:
ans += 1
return ans
Rust
impl Solution {
pub fn find_lonely_pixel(pic: Vec<Vec<char>>) -> i32 {
let m = pic.len();
let n = pic[0].len();
let mut row = vec![0; m];
let mut col = vec![0; n];
for i in 0..m {
for j in 0..n {
if pic[i][j] == 'B' {
row[i] += 1;
col[j] += 1;
}
}
}
let mut ans = 0;
for i in 0..m {
for j in 0..n {
if pic[i][j] == 'B' && row[i] == 1 && col[j] == 1 {
ans += 1;
}
}
}
ans
}
}
TypeScript
class Solution {
findLonelyPixel(pic: string[][]): number {
const m = pic.length, n = pic[0].length;
const row = Array(m).fill(0), col = Array(n).fill(0);
for (let i = 0; i < m; i++)
for (let j = 0; j < n; j++)
if (pic[i][j] === 'B') { row[i]++; col[j]++; }
let ans = 0;
for (let i = 0; i < m; i++)
for (let j = 0; j < n; j++)
if (pic[i][j] === 'B' && row[i] === 1 && col[j] === 1) ans++;
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(mn), where m and n are the dimensions of the matrix. We scan the matrix twice. - 🧺 Space complexity:
O(m+n), for the row and column count arrays.