LC: 531. Lonely Pixel I - spiralgo/algorithms GitHub Wiki

531. Lonely Pixel I

The Essence:

The definition of lonely pixel is sufficient for solving this problem:

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.

The number of black pixels on each row and column can be counted separately. After the counting, if a black pixel's row and column count are both equal to 1, then it can be counted as lonely.

As a general note, sometimes problems do explicitly show how they are solved. It's only left to the problem-solver to notice this and implement the proper solution.

Details:

For this algorithm, the pixels matrix needs to be looped through twice: first for counting, second for comparing. For the counting, the algorithm needs two 1D arrays for row and column counts.

The code for it can be found here: https://github.com/spiralgo/algorithms/blob/master/src/main/java/algorithms/curated170/medium/LonelyPixel1.java