FastPrepLargest All-One Square Area

Largest All-One Square Area

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You are given a nonempty rectangular binary matrix matrix. Find the largest square whose cells all contain 1, and return its area.

A square uses consecutive rows and consecutive columns. Return 0 when the matrix contains no 1.

Function

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

Examples

Example 1

matrix = [[1,1,1],[1,1,1]]return = 4

A square can have side length 2, even though the all-one rectangle is wider.

Example 2

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

There are two overlapping squares of side length 2; the whole matrix is not all ones.

Example 3

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

No all-one cell exists, so the largest square has area 0.

Constraints

  • 1 <= matrix.length, matrix[0].length <= 300.
  • Every row has the same length.
  • Every cell is 0 or 1.

More Google problems

See Google hiring insights
public int maximalSquareArea(int[][] matrix) {
    // Write your code here
}
matrix[[1,1,1],[1,1,1]]
expected4
Checking account…