LeetCode 531: Lonely Pixel I Solution

Master LeetCode problem 531 (Lonely Pixel I), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

531. Lonely Pixel I

Problem Explanation

Explanation

The problem asks us to find the number of lonely pixels in a black-and-white image. A lonely pixel is defined as a black pixel (represented by 'B') that is the only one in its row and column.

To solve this problem, we can iterate through the image and count the number of black pixels in each row and column. Then, we iterate through the image again and check if a black pixel is the only one in its row and column. If it is, we increment a counter for lonely pixels.

Algorithm:

  1. Initialize arrays rowCount and colCount to store counts of black pixels in each row and column, respectively.
  2. Iterate through the image to count black pixels in each row and column and store the counts in rowCount and colCount.
  3. Iterate through the image again and for each black pixel, check if it is the only one in its row and column. If so, increment the counter for lonely pixels.
  4. Return the count of lonely pixels.

Time Complexity:

  • O(m * n) where m and n are the number of rows and columns in the image.

Space Complexity:

  • O(m + n) for the rowCount and colCount arrays.

Solution Code

class Solution {
    public int findLonelyPixel(char[][] picture) {
        int m = picture.length, n = picture[0].length;
        int[] rowCount = new int[m];
        int[] colCount = new int[n];
        
        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;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (picture[i][j] == 'B' && rowCount[i] == 1 && colCount[j] == 1) {
                    lonelyPixelCount++;
                }
            }
        }
        
        return lonelyPixelCount;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 531 (Lonely Pixel I)?

This page provides optimized solutions for LeetCode problem 531 (Lonely Pixel I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 531 (Lonely Pixel I)?

The time complexity for LeetCode 531 (Lonely Pixel I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 531 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 531 in Java, C++, or Python.

Back to LeetCode Solutions