Lonely Pixel II - Problem

Imagine you're analyzing a digital image represented as a matrix of black ('B') and white ('W') pixels. Your task is to find lonely black pixels - special black pixels that meet very specific criteria!

A black lonely pixel is a 'B' located at position (r, c) where:

  • Both row r and column c contain exactly target black pixels
  • All rows that have a black pixel at column c must be identical to row r

Goal: Count how many black lonely pixels exist in the given m ร— n picture.

Example: If target = 3, a black pixel is lonely only if its row and column both have exactly 3 black pixels, and any other rows with black pixels in the same column are identical to this row.

Input & Output

example_1.py โ€” Basic Case
$ 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","B"]], target = 3
โ€บ Output: 6
๐Ÿ’ก Note: The first three rows are identical and each has 3 black pixels. For columns 1, 3, 4: each has exactly 3 black pixels and all blacks in these columns come from identical rows. Each black pixel in these positions is lonely, giving us 3 rows ร— 3 columns = 6 lonely pixels.
example_2.py โ€” No Lonely Pixels
$ Input: picture = [["W","W","B"],["W","W","B"],["W","W","B"]], target = 1
โ€บ Output: 0
๐Ÿ’ก Note: Column 2 has 3 black pixels, but target is 1, so no black pixel can be lonely. Even though each row has exactly 1 black pixel, the column condition fails.
example_3.py โ€” Different Row Patterns
$ Input: picture = [["B","W"],["W","B"]], target = 1
โ€บ Output: 0
๐Ÿ’ก Note: Each row and column has exactly 1 black pixel, but the rows with black pixels in the same column are different (["B","W"] vs ["W","B"]), so no pixels are lonely.

Constraints

  • m == picture.length
  • n == picture[i].length
  • 1 โ‰ค m, n โ‰ค 200
  • picture[i][j] is either 'W' or 'B'
  • 1 โ‰ค target โ‰ค min(m, n)

Visualization

Tap to expand
๐ŸŽฏ Security Camera Grid: Finding Lonely PixelsStep 1-2: Group & FilterHash Map GroupsPattern "Bโ—โ—": 3 cameras โœ“Pattern "Bโ—โ—": 1 camera โœ—Step 3: Column AnalysisColumn CheckColumn has 3 camerasTarget = 3 โœ“Step 4-5: Validate & CountFinal Validationโœ“ Same row patternโœ“ Target row blacksโœ“ Target column blacksResult: 6 Lonely PixelsAlgorithm VisualizationInput Matrix:Row 0: W B W (1 black)Row 1: W B W (1 black)Row 2: W B W (1 black)Hash Groups:"WBW": [0,1,2] โ†’ 3 rowsEach row has 1 black (target=1 โœ“)Column 1 has 3 blacks (target=1 โœ—)Result: 0 lonely pixels
Understanding the Visualization
1
Group Identical Patterns
Use hash map to group rows with identical camera arrangements
2
Filter Valid Groups
Keep only groups where each row has exactly target cameras
3
Column Analysis
For each column position with cameras, count total cameras in that column
4
Validate Consistency
Ensure all cameras in a column come from the same row pattern
5
Count Lonely Cameras
Sum up all valid cameras meeting all conditions
Key Takeaway
๐ŸŽฏ Key Insight: Using hash map to group identical row patterns eliminates redundant comparisons and enables O(mn) solution by validating row consistency efficiently.
Asked in
Google 42 Amazon 28 Meta 35 Microsoft 31
23.6K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen