Skip to content

Maximal Rectangle

HardStack
Decode it first. Clues, constraints, pattern and plan before any code.

Given a grid matrix filled with the characters "0" and "1", return the area of the largest rectangle containing only "1"s. The rectangle's sides follow the grid lines.

matrix = [
  ["1","0","1","0","0"],
  ["1","0","1","1","1"],
  ["1","1","1","1","1"],
  ["1","0","0","1","0"],
]
→  6   (rows 1 and 2, columns 2 to 4)
 
matrix = [["0"]]  →  0
matrix = [["1"]]  →  1

Constraints

  • rows == matrix.length, cols == matrix[i].length
  • 1 <= rows, cols <= 200
  • matrix[i][j] is "0" or "1".