FastPrepMaximum All-Ones Square Side Length
Problem · Array

Maximum All-Ones Square Side Length

Learn this problem
MediumOmnissa logoOmnissaFULLTIMEONSITE INTERVIEW

Problem 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[][]) → int

Examples

Example 1

matrix = [[1,0,1,1],[1,1,1,1],[1,1,1,0]]return = 2

Rows 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 = 0

There are no one-cells, so no positive-size all-ones square exists.

Constraints

  • 1 <= matrix.length <= 50
  • 1 <= matrix[0].length <= 50
  • All rows have the same length.
  • Every matrix element is 0 or 1.

More Omnissa problems

drafts saved locally
public int largestSquare(int[][] matrix) {
    // Write your code here
}
matrix[[1,0,1,1],[1,1,1,1],[1,1,1,0]]
expected2
checking account