Given a matrix of 1s and 0s, find the coordinates of the top-left and bottom-right corners of a rectangle made entirely of 0s.
The matrix is guaranteed to contain exactly one rectangle of 0s. All other cells in the matrix are 1s.
Return the result as a list of two coordinates: [[r1, c1], [r2, c2]], where (r1, c1) is the top-left corner and (r2, c2) is the bottom-right corner of the rectangle.
Function Signature
def find_rectangle(matrix: list[list[int]]) -> list[list[int]]:
Rules
-
Coordinates are 0-based
[row, column]
pairs.
-
The
0
cells form one solid, axis-aligned rectangle with at least one cell. It can be a single cell, a single row or a single column, and it can touch any edge of the matrix.
-
Both corners are inclusive:
(r1, c1)
and
(r2, c2)
are themselves
0
cells.
Constraints
-
1 <= m, n <= 100
, where
m
is the number of rows and
n
is the number of columns
-
matrix[i][j]
is either
0
or
1
-
There is exactly one rectangle of
0
s in the matrix
Examples
Input: matrix = [[1,1,1,1,1,1],[0,0,1,1,1,1],[0,0,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
Output: [[1,0],[2,1]]
The zeros fill rows 1 to 2 and columns 0 to 1, so the top-left corner is (1, 0) and the bottom-right corner is (2, 1).
Input: matrix = [[1,1,1,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,1,1,1]]
Output: [[1,1],[3,2]]
The zeros fill rows 1 to 3 and columns 1 to 2, which gives the corners (1, 1) and (3, 2).
Input: matrix = [[1,1,1],[1,0,0]]
Output: [[1,1],[1,2]]
The rectangle is one row tall and runs into the last row and the last column, so its bottom-right corner (1, 2) sits on the border of the matrix.