FastPrepMaximal Rectangle in a Binary Matrix

Maximal Rectangle in a Binary Matrix

Google logoGoogle● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a rectangular binary matrix matrix, return the area of the largest axis-aligned rectangle composed only of 1 values.

A rectangle may span any positive number of consecutive rows and columns. Return 0 when the matrix contains no 1.

Function

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

Examples

Example 1

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

The largest all-one rectangle has height 2 and width 3.

Example 2

matrix = [[0]]return = 0

No all-one rectangle exists.

Example 3

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

The entire matrix is an all-one rectangle of area 4.

Constraints

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

More Google problems

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