LeetCode 533: Lonely Pixel II Solution

Master LeetCode problem 533 (Lonely Pixel II), 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.

533. Lonely Pixel II

Problem Explanation

Explanation

The problem "Lonely Pixel II" asks us to determine the number of "black" pixels in a given picture that meet specific criteria. A black pixel is represented as 'B' and a white pixel is represented as 'W'. The criteria for a black pixel to be considered "lonely" are:

  1. There must be exactly one black pixel in the same row as the given black pixel.
  2. There must be exactly one black pixel in the same column as the given black pixel.

To solve this problem efficiently, we can follow these steps:

  1. Count the number of black pixels in each row and each column.
  2. For each black pixel, check if it is lonely based on the above criteria.
  3. Count the total number of lonely black pixels.

Time complexity: O(mn) where m is the number of rows and n is the number of columns in the picture. Space complexity: O(m + n) to store the counts of black pixels in each row and each column.

Solution Code

class Solution {
    public int findBlackPixel(char[][] picture, int N) {
        int m = picture.length;
        int n = picture[0].length;
        int[] rowCount = new int[m];
        int[] colCount = new int[n];
        Map<String, Integer> map = new HashMap<>();

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (picture[i][j] == 'B') {
                    rowCount[i]++;
                    colCount[j]++;
                }
            }
        }

        for (int i = 0; i < m; i++) {
            if (rowCount[i] == N) {
                String key = Arrays.toString(picture[i]);
                map.put(key, map.getOrDefault(key, 0) + 1);
            }
        }

        int count = 0;
        for (int j = 0; j < n; j++) {
            if (colCount[j] == N) {
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if (entry.getValue() == N && entry.getKey().charAt(j) == 'B') {
                        count += N;
                    }
                }
            }
        }

        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 533 (Lonely Pixel II)?

This page provides optimized solutions for LeetCode problem 533 (Lonely Pixel II) 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 533 (Lonely Pixel II)?

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

Can I run code for LeetCode 533 on DevExCode?

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

Back to LeetCode Solutions