Smallest Rectangle Enclosing Black Pixels Problem
Description
LeetCode Problem 302.
You are given an m x n binary matrix image where 0 represents a white pixel and 1 represents a black pixel.
The black pixels are connected (i.e., there is only one black region). Pixels are connected horizontally and vertically.
Given two integers x and y that represents the location of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.
You must write an algorithm with less than O(mn) runtime complexity
Example 1:
![]()
Input: image = [["0","0","1","0"],["0","1","1","0"],["0","1","0","0"]], x = 0, y = 2
Output: 6
Example 2:
Input: image = [["1"]], x = 0, y = 0
Output: 1
Constraints:
m == image.lengthn == image[i].length1 <= m, n <= 100image[i][j]is either'0'or'1'.0 <= x < m0 <= y < nimage[x][y] == '1'.- The black pixels in the
imageonly form one component.
Solution
Approach: Binary Search on the Four Edges
Scanning every pixel is O(mn), and the problem asks for less. The fact that the black pixels form one connected region gives us a monotone structure to binary search on.
Project the image onto the rows: mark each row that contains at least one black pixel. Because the region is connected, these rows form one unbroken block (a connected region cannot skip a row), and that block contains row x, since (x, y) is black. The same holds for the columns and column y.
So the four edges of the rectangle can be found with four binary searches:
- top: the first row in
[0, x)that has a black pixel (orxitself if none). Above the block every row is white, inside it every row has black, so the "has black" test is false and then true: a binary search finds the switch. - bottom: the first row in
[x + 1, m)that has no black pixel (orm). Here the test goes from true to false. - left and right: the same two searches on the columns, around
y.
Each test scans one row (n pixels) or one column (m pixels). The area is (bottom - top) * (right - left), where bottom and right are one past the last black row and column.
Without connectivity this would fail: an all-white row could separate two black parts, and the binary search could stop at that gap.
Complexity: O(m log n + n log m) time: about log m row checks of n pixels each, and log n column checks of m pixels each. O(1) extra space.
class Solution:
def minArea(self, image: List[List[str]], x: int, y: int) -> int:
m, n = len(image), len(image[0])
def row_has_black(i: int) -> bool:
return "1" in image[i]
def col_has_black(j: int) -> bool:
return any(image[i][j] == "1" for i in range(m))
def first(lo: int, hi: int, has_black, want: bool) -> int:
"""The first index in [lo, hi) where has_black(index) == want (hi if none)."""
while lo < hi:
mid = (lo + hi) // 2
if has_black(mid) == want:
hi = mid
else:
lo = mid + 1
return lo
# The black region is connected, so the rows (columns) that contain black pixels are
# consecutive and include row x (column y): binary search for each of the four edges.
top = first(0, x, row_has_black, True)
bottom = first(x + 1, m, row_has_black, False) # first row below with no black pixel
left = first(0, y, col_has_black, True)
right = first(y + 1, n, col_has_black, False)
return (bottom - top) * (right - left)