MediumNo. 0533LeetCode Premium

Lonely Pixel II — LeetCode 533 Python Solution

Topics
Array, Hash Table, Matrix
Reading time
3 min

The problem

Given an m x n picture consisting of black 'B' and white 'W' pixels and an integer target, return the number of black lonely pixels. A black lonely pixel is a character 'B' that located at a specific position (r, c) where: Row r and column c both contain exactly target black pixels.

Example

Input
picture = [["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","W","B","W","B","W"]], target = 3
Output
6
Explanation
All the green 'B' are the black pixels we need (all 'B's at column 1 and 3).

Python solution

PythonSolution
class Solution:
    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
        rows = [0] * len(picture)
        g = defaultdict(list)
        for i, row in enumerate(picture):
            for j, x in enumerate(row):
                if x == "B":
                    rows[i] += 1
                    g[j].append(i)
        ans = 0
        for j in g:
            i1 = g[j][0]
            if rows[i1] != target:
                continue
            if len(g[j]) == rows[i1] and all(picture[i2] == picture[i1] for i2 in g[j]):
                ans += target
        return ans

Complexity

Time
O(m × n2)
Space · auxiliary
O(m × n)where m and n are the number of rows and columns in the matrix respectively

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 533. Lonely Pixel II is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.

The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0036Valid SudokuMedium
0037Sudoku SolverHard
0073Set Matrix ZeroesMedium
0840Magic Squares In GridMedium
0959Regions Cut By SlashesMedium
1072Flip Columns For Maximum Number of Equal RowsMedium

Frequently asked questions

How hard is LeetCode 533. Lonely Pixel II?

LeetCode 533. Lonely Pixel II is rated Medium on LeetCode.

What is the time complexity of LeetCode 533. Lonely Pixel II?

The Python solution on this page runs in O(m × n2).

What is the space complexity of LeetCode 533. Lonely Pixel II?

The Python solution on this page uses O(m × n), where m and n are the number of rows and columns in the matrix respectively auxiliary space.

What topics does LeetCode 533. Lonely Pixel II cover?

LeetCode 533. Lonely Pixel II is tagged Array, Hash Table and Matrix on LeetCode.

Is LeetCode 533. Lonely Pixel II a premium problem?

Yes. LeetCode 533. Lonely Pixel II is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.