531. Lonely Pixel I
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
A "lonely pixel" is defined as a black pixel ('B') that is unique in its row and column. This means for a pixel at (r, c) to be lonely, it must be 'B', and there should be no other 'B's in row r and no other 'B's in column c. The problem asks us to count the total number of such lonely pixels in a given m x n grid.
Solution
Lonely Pixel I in Python
class Solution:
def findLonelyPixel(self, picture: List[List[str]]) -> int:
if not picture or not picture[0]:
return 0
m, n = len(picture), len(picture[0])
# Arrays to store the count of black pixels in each row and column.
rowCount = [0] * m
colCount = [0] * n
# First pass: iterate through the picture to count black pixels.
for r in range(m):
for c in range(n):
if picture[r][c] == 'B':
rowCount[r] += 1
colCount[c] += 1
lonely_pixel_count = 0
# Second pass: iterate again to find lonely pixels.
# A pixel is lonely if it's 'B' and its row and column contain exactly one 'B'.
for r in range(m):
for c in range(n):
if picture[r][c] == 'B':
if rowCount[r] == 1 and colCount[c] == 1:
lonely_pixel_count += 1
return lonely_pixel_count
Lonely Pixel I in Java
class Solution {
public int findLonelyPixel(char[][] picture) {
if (picture == null || picture.length == 0 || picture[0].length == 0) {
return 0;
}
int m = picture.length;
int n = picture[0].length;
// Arrays to store the count of black pixels in each row and column.
int[] rowCount = new int[m];
int[] colCount = new int[n];
// First pass: iterate through the picture to count black pixels.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (picture[i][j] == 'B') {
rowCount[i]++;
colCount[j]++;
}
}
}
int lonelyPixelCount = 0;
// Second pass: iterate again to find lonely pixels.
// A pixel is lonely if it's 'B' and its row and column contain exactly one 'B'.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (picture[i][j] == 'B') {
if (rowCount[i] == 1 && colCount[j] == 1) {
lonelyPixelCount++;
}
}
}
}
return lonelyPixelCount;
}
}
Lonely Pixel I in C++
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
if (picture.empty() || picture[0].empty()) {
return 0;
}
int m = picture.size();
int n = picture[0].size();
// Arrays to store the count of black pixels in each row and column.
vector<int> rowCount(m, 0);
vector<int> colCount(n, 0);
// First pass: iterate through the picture to count black pixels.
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
rowCount[i]++;
colCount[j]++;
}
}
}
int lonelyPixelCount = 0;
// Second pass: iterate again to find lonely pixels.
// A pixel is lonely if it's 'B' and its row and column contain exactly one 'B'.
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
if (rowCount[i] == 1 && colCount[j] == 1) {
lonelyPixelCount++;
}
}
}
}
return lonelyPixelCount;
}
};
Lonely Pixel I in TypeScript
function findLonelyPixel(picture: string[][]): number {
if (!picture || picture.length === 0 || picture[0].length === 0) {
return 0;
}
const m = picture.length;
const n = picture[0].length;
// Arrays to store the count of black pixels in each row and column.
const rowCount: number[] = new Array(m).fill(0);
const colCount: number[] = new Array(n).fill(0);
// First pass: iterate through the picture to count black pixels.
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (picture[i][j] === 'B') {
rowCount[i]++;
colCount[j]++;
}
}
}
let lonelyPixelCount = 0;
// Second pass: iterate again to find lonely pixels.
// A pixel is lonely if it's 'B' and its row and column contain exactly one 'B'.
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (picture[i][j] === 'B') {
if (rowCount[i] === 1 && colCount[j] === 1) {
lonelyPixelCount++;
}
}
}
}
return lonelyPixelCount;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.