Problem · Array
Maximum All-Ones Square Side Length
Learn this problemProblem statement
Given a non-empty rectangular binary matrix matrix, return the largest side length of an axis-aligned square whose every cell is 1.
A square uses consecutive rows and consecutive columns. Its entire interior, not only its border, must contain ones. Return 0 if the matrix contains no ones. Return the side length, not the area; do not rotate the square.
Function
largestSquare(matrix: int[][]) → intExamples
Example 1
matrix = [[1,0,1,1],[1,1,1,1],[1,1,1,0]]return = 2Rows 1 through 2 and columns 0 through 1 form a 2-by-2 all-ones square (zero-based indices). Every possible 3-by-3 square contains a zero, so return side length 2.
Example 2
matrix = [[0,0],[0,0]]return = 0There are no one-cells, so no positive-size all-ones square exists.
Constraints
1 <= matrix.length <= 501 <= matrix[0].length <= 50- All rows have the same length.
- Every matrix element is
0or1.