Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of lonely pixels.
A lonely pixel is a black 'B' pixel that satisfies these conditions:
Example 1:
Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]]Output: 3Explanation: All the three 'B's are lonely pixels.
Example 2:
Input: picture = [["B","B","B"],["B","B","W"],["B","B","B"]]Output: 0
Constraints:
m == picture.lengthn == picture[i].length1 <= m, n <= 500picture[i][j] is 'W' or 'B'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find unique bright pixels in a picture. The brute force method involves examining each bright pixel and comparing it with every other pixel in its row and column to see if it's truly unique.
Here's how the algorithm would work step-by-step:
def lonely_pixel_i_brute_force(picture):
number_of_rows = len(picture)
number_of_cols = len(picture[0]) if number_of_rows > 0 else 0
lonely_pixel_count = 0
for row_index in range(number_of_rows):
for col_index in range(number_of_cols):
if picture[row_index][col_index] == 'B':
is_lonely = True
# Check for other bright pixels in the same row.
for other_col_index in range(number_of_cols):
if other_col_index != col_index and picture[row_index][other_col_index] == 'B':
is_lonely = False
break
if is_lonely:
# Check for other bright pixels in the same column.
for other_row_index in range(number_of_rows):
if other_row_index != row_index and picture[other_row_index][col_index] == 'B':
is_lonely = False
break
if is_lonely:
lonely_pixel_count += 1
return lonely_pixel_countTo find the lonely pixel, we don't need to check every single pixel. The trick is to focus on rows and columns. We only care about rows and columns with exactly one black pixel.
Here's how the algorithm would work step-by-step:
def findLonelyPixel(picture):
number_of_rows = len(picture)
number_of_cols = len(picture[0])
row_counts = [0] * number_of_rows
col_counts = [0] * number_of_cols
# Count black pixels in each row
for row_index in range(number_of_rows):
for col_index in range(number_of_cols):
if picture[row_index][col_index] == 'B':
row_counts[row_index] += 1
# Count black pixels in each column
for col_index in range(number_of_cols):
for row_index in range(number_of_rows):
if picture[row_index][col_index] == 'B':
col_counts[col_index] += 1
lonely_pixel_count = 0
# Only count pixels where both row and column have one black pixel
for row_index in range(number_of_rows):
for col_index in range(number_of_cols):
if picture[row_index][col_index] == 'B':
#Check that pixel's row and column have exactly one 'B'
if row_counts[row_index] == 1 and col_counts[col_index] == 1:
lonely_pixel_count += 1
return lonely_pixel_count| Case | How to Handle |
|---|---|
| Null or empty image input | Return 0 immediately, as there are no pixels to analyze. |
| Image with zero rows or zero columns | Return 0, as the image is effectively empty. |
| Image with only one row or only one column | Iterate through the single row or column and check if any 'B' appears exactly once. |
| Image with all 'W' pixels | Return 0, as there are no black pixels to consider. |
| Image with all 'B' pixels | Return 0, as no pixel will be the only one in its row or column unless the image is 1x1. |
| Image with a single 'B' pixel | Return 1, as this pixel is the only one in its row and column. |
| Large image (e.g., 200x200) to test performance | Ensure the solution uses an efficient algorithm (e.g., O(m*n)) to avoid timeouts. |
| Integer overflow when counting black pixels in a row or column | Since the problem statement constraints limit row and column size, integer overflow isn't a practical concern. |