Lonely Pixel II Problem
Description
LeetCode Problem 533.
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
rand columncboth contain exactlytargetblack pixels. - For all rows that have a black pixel at column
c, they should be exactly the same as rowr.
Example 1:
![]()
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.
Example 2:
![]()
Input: picture = [["W","W","B"],["W","W","B"],["W","W","B"]], target = 1
Output: 0
Constraints:
m == picture.lengthn == picture[i].length1 <= m, n <= 200picture[i][j]is'W'or'B'.1 <= target <= min(m, n)
Solution
Approach: Count Rows, Group by Column
Look at the second rule from the point of view of a column c: every row with a black pixel in column c must be identical to row r. Since r itself is one of those rows, this simply says that all rows with a black pixel in column c are the same. That condition depends only on the column, not on which black pixel of the column we pick. The same is true of the first rule: if the rows are identical, they all have the same number of black pixels.
So a column either makes all of its black pixels lonely or none of them, and we can decide column by column:
- Count the black pixels of each row, and for each column collect the list of rows that have a black pixel there.
- For a column, its list must have exactly
targetrows (the column count), and the first of those rows must have exactlytargetblack pixels (the row count). - Compare every row in the list with the first one. If they are all equal, every one of the
targetblack pixels in this column is lonely: addtargetto the answer.
In Example 1, columns 1 and 3 each have black pixels in rows 0, 1 and 2, which are identical and contain 3 black pixels each, so they contribute 3 + 3 = 6. Column 4 also has 3 black pixels, but row 3 differs from row 0, so it contributes nothing.
The easy mistake is to check only the counts: rows with the same number of black pixels in different places do not satisfy the second rule.
Complexity: O(mn2) time in the worst case: counting takes O(mn), and each of the n columns may compare up to target rows of length n. O(mn) space for the row lists.
class Solution:
def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
rows = [0] * len(picture) # black pixels in each row
col_rows = [[] for _ in picture[0]] # rows with a black pixel in each column
for i, row in enumerate(picture):
for j, x in enumerate(row):
if x == "B":
rows[i] += 1
col_rows[j].append(i)
ans = 0
for black_rows in col_rows:
if len(black_rows) != target:
continue
first = black_rows[0]
# all these rows must equal the first one, which must hold target blacks
if rows[first] == target and all(picture[i] == picture[first] for i in black_rows):
ans += target # every black pixel of this column is lonely
return ans