You are given a 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 the location (x, y) 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 O(m log n + n log m) runtime complexity where m is the number of rows and n is the number of columns.
Example 1:
Input: image = [["0","0","1","0"],["0","1","1","0"],["0","1","0","0"]], x = 0, y = 2
Output: 6
Explanation: The smallest rectangle enclosing the black pixels is shown in the following image.
Example 2:
Input: image = [["1"]], x = 0, y = 0
Output: 1
Example 3:
Input: image = [["0","1","0"],["1","1","1"],["0","1","0"]], x = 1, y = 1
Output: 3
Constraints:
m == image.lengthn == image[i].length1 <= m, n <= 100image[i][j] is either '0' or '1'.1 <= the number of black pixels <= 1000 <= x < m0 <= y < nimage[x][y] == '1'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:
The problem is to find the smallest rectangle that contains all the black pixels in an image. A brute force approach involves checking every possible rectangle that could exist within the image to see if it fits our criteria.
Here's how the algorithm would work step-by-step:
def smallest_rectangle_enclosing_black_pixels_brute_force(image):
rows = len(image)
cols = len(image[0]) if rows > 0 else 0
min_area = float('inf')
for top_row in range(rows):
for left_col in range(cols):
for bottom_row in range(top_row, rows):
for right_col in range(left_col, cols):
# Iterate all possible rectangles
is_valid_rectangle = True
has_black_pixel = False
for row in range(top_row, bottom_row + 1):
for col in range(left_col, right_col + 1):
if image[row][col] == '1':
has_black_pixel = True
break
if has_black_pixel:
break
if not has_black_pixel:
continue
# We need to ensure there are no white pixels inside the rectangle
for row in range(rows):
for col in range(cols):
if image[row][col] == '1':
if not (top_row <= row <= bottom_row and \
left_col <= col <= right_col):
is_valid_rectangle = False
break
if not is_valid_rectangle:
break
if is_valid_rectangle:
# Update min_area only if the rectangle is valid
area = (bottom_row - top_row + 1) * (right_col - left_col + 1)
min_area = min(min_area, area)
if min_area == float('inf'):
# No black pixels found
return 0
else:
return min_areaThe problem asks for the smallest rectangle that contains all the black pixels in an image. The most efficient strategy is to use a targeted search to quickly find the boundaries of this rectangle without looking at every single pixel individually.
Here's how the algorithm would work step-by-step:
def smallest_rectangle_enclosing_black_pixels(image):
number_of_rows = len(image)
number_of_columns = len(image[0])
def find_top_edge():
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
if image[row_index][column_index] == '1':
return row_index
def find_bottom_edge():
for row_index in range(number_of_rows - 1, -1, -1):
for column_index in range(number_of_columns):
if image[row_index][column_index] == '1':
return row_index
# Finding the leftmost column with a black pixel.
def find_left_edge():
for column_index in range(number_of_columns):
for row_index in range(number_of_rows):
if image[row_index][column_index] == '1':
return column_index
def find_right_edge():
# Searching right to left.
for column_index in range(number_of_columns - 1, -1, -1):
for row_index in range(number_of_rows):
if image[row_index][column_index] == '1':
return column_index
top_edge = find_top_edge()
bottom_edge = find_bottom_edge()
left_edge = find_left_edge()
right_edge = find_right_edge()
# Calculate the area using the boundaries.
return (right_edge - left_edge + 1) * (bottom_edge - top_edge + 1)| Case | How to Handle |
|---|---|
| Null or empty image input | Return 0 immediately as there are no pixels to enclose. |
| Image with zero rows or zero columns | Return 0 immediately, similar to a null or empty image. |
| Image with all white pixels | Return 0 since there are no black pixels to form a rectangle. |
| Image with only one black pixel | Return 1, as the smallest enclosing rectangle would be of size 1x1. |
| Image with all black pixels | Return the total area of the image (rows * cols). |
| Single row or single column image with black pixels | Find the first and last black pixel and calculate the length of the rectangle. |
| Large image where the recursion depth could exceed the stack limit (if using recursive binary search) | Convert to an iterative binary search to avoid stack overflow errors. |
| Integer overflow when calculating the area of the rectangle (especially with very large images) | Use a larger data type (e.g., long) for storing the area to prevent potential overflow. |