Smallest Rectangle Enclosing Black Pixels

Try to solve the Smallest Rectangle Enclosing Black Pixels problem.

Statement

You are given an m×nm \times n binary matrix image, where 00 represents a white pixel and 11 represents a black pixel.

All black pixels in the matrix form a single connected region, where connectivity is defined by horizontal or vertical adjacency.

Given two integers x and y that represent the coordinates of one of the black pixels, write an algorithm to find the area of the smallest axis-aligned rectangle that encloses all the black pixels.

Note: Your solution must have a runtime complexity less than O(m×n)O(m \times n).

Constraints:

  • mm ==== image.length

  • nn == image[i].length

  • 1m,n1001 \leq m, n \leq 100

  • image[i][j] is either 00 or 11

  • 00 \leq x <m< m

  • 00 \leq y <n< n

  • image[x][y] ==1== 1

  • The black pixels in the image only form one component.

Examples

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.