FastPrepLargest Square of Ones

Largest Square of Ones

OpenAI logoOpenAI● MediumFULLTIMEOA
Learn

Problem statement

Given a rectangular binary matrix matrix, return the side length of the largest square submatrix composed only of 1 values.

If the matrix contains no 1, return 0.

Function

findLargestSquare(matrix: int[][]) → int

Examples

Example 1

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

The rows and columns from index 1 through 3 contain a 3 x 3 square of ones.

Example 2

matrix = [[0,0],[0,0]]return = 0

No cell contains 1.

Example 3

matrix = [[1]]return = 1

The single cell is itself a square of side length 1.

Constraints

  • 1 <= matrix.length <= 500.
  • 1 <= matrix[i].length <= 500.
  • Every row has the same length.
  • Every value is either 0 or 1.

More OpenAI problems

See OpenAI hiring insights
public int findLargestSquare(int[][] matrix) {
    // Write your code here.
}
matrix[[1,1,0,1,0],[1,1,1,1,1],[0,0,1,1,1],[0,0,1,1,1],[1,1,0,0,0]]
expected3
Checking account…