Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #533: Lonely Pixel II

In this guide, we solve Leetcode #533 Lonely Pixel II in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Array, Hash Table, Matrix

Intuition

Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.

By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.

Approach

Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.

This keeps the solution linear while remaining easy to explain in an interview setting.

Steps:

  • Initialize a hash map for seen items or counts.
  • Iterate through the input, querying/updating the map.
  • Return the first valid result or the final computed value.

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). Take 'B' at row r = 0 and column c = 1 as an example: - Rule 1, row r = 0 and column c = 1 both have exactly target = 3 black pixels. - Rule 2, the rows have black pixel at column c = 1 are row 0, row 1 and row 2. They are exactly the same as row r = 0.

Python Solution

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

The time complexity is O(m×n2)O(m \times n^2)O(m×n2), and the space complexity is O(m×n)O(m \times n)O(m×n), where mmm and nnn are the number of rows and columns in the matrix respectively. The space complexity is O(m×n)O(m \times n)O(m×n), where mmm and nnn are the number of rows and columns in the matrix respectively.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy