Smallest Rectangle Enclosing Black Pixels - Problem

Imagine you're working with a digital photograph represented as a binary matrix where 0 represents white pixels and 1 represents black pixels. You know that all black pixels form a single connected region (like a blob of ink on paper).

Your task is to find the smallest axis-aligned rectangle that completely encloses all the black pixels. Think of it like drawing the tightest possible rectangular frame around a shape.

Key constraints:

  • The matrix is m x n in size
  • All black pixels (1s) are connected horizontally or vertically
  • You're given the coordinates (x, y) of one black pixel as a starting point
  • Your algorithm must run in less than O(mn) time complexity

Return the area of the smallest rectangle that encloses all black pixels.

Input & Output

example_1.py โ€” Basic Rectangle
$ Input: image = [["0","0","1","0"],["0","1","1","0"],["0","1","0","0"]], x = 0, y = 2
โ€บ Output: 6
๐Ÿ’ก Note: The black pixels form a shape from row 0-2 and columns 1-2, creating a 3ร—2 rectangle with area 6
example_2.py โ€” Single Column
$ Input: image = [["1"],["1"],["1"]], x = 1, y = 0
โ€บ Output: 3
๐Ÿ’ก Note: All black pixels are in a single column, forming a 3ร—1 rectangle with area 3
example_3.py โ€” L-shaped Region
$ Input: image = [["1","1","0"],["1","0","0"],["1","0","0"]], x = 0, y = 0
โ€บ Output: 6
๐Ÿ’ก Note: The L-shaped black region requires a 3ร—2 bounding rectangle with area 6

Visualization

Tap to expand
Binary Search Solution VisualizationOriginal MatrixStart (0,2)Rectangle: 4ร—3 = 12 areaBinary Search ProcessSearch Strategy:Left boundary: cols [0,2] โ†’ find 1Right boundary: cols [2,5] โ†’ find 3Top boundary: rows [0,0] โ†’ find 0Bottom boundary: rows [0,3] โ†’ find 2Result: (2-0+1) ร— (3-1+1) = 3 ร— 3 = 9Time: O(m log n + n log m) vs O(mn)๐Ÿ’ก Key InsightBinary search beats brute force: O(log n) vs O(n)
Understanding the Visualization
1
Identify the Subject
We know there's black ink at position (x,y) - this is our reference point
2
Smart Boundary Detection
Instead of checking every pixel, use binary search to quickly find edges
3
Search Left Edge
Binary search columns from 0 to y to find leftmost black pixels
4
Search Right Edge
Binary search columns from y to n-1 to find rightmost black pixels
5
Search Vertical Edges
Similarly find top and bottom boundaries using row searches
6
Calculate Frame Size
Multiply width ร— height to get the minimum rectangle area
Key Takeaway
๐ŸŽฏ Key Insight: By leveraging binary search and the connected region property, we can find rectangle boundaries in logarithmic time instead of linear time, achieving the required sub-O(mn) complexity while maintaining correctness.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m log n + n log m)

Four binary searches: 2 on columns (m comparisons each) and 2 on rows (n comparisons each)

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using a few variables for binary search bounds

n
2n
โœ“ Linear Space

Constraints

  • m == image.length
  • n == image[i].length
  • 1 โ‰ค m, n โ‰ค 100
  • image[i][j] is either '0' or '1'
  • 0 โ‰ค x < m
  • 0 โ‰ค y < n
  • image[x][y] == '1'
  • The black pixels in the image only form one component
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
23.5K Views
Medium Frequency
~25 min Avg. Time
892 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