Skip to content

Maximal Square

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

You are given a grid matrix filled with the strings "0" and "1". Find the largest square containing only "1"s and return its area.

matrix = [["1","0","1","0","0"],
          ["1","0","1","1","1"],
          ["1","1","1","1","1"],
          ["1","0","0","1","0"]]   →  4   (a 2 × 2 square)
 
matrix = [["0","1"],
          ["1","0"]]                 →  1

Constraints

  • 1 <= m, n <= 300 where the grid is m × n
  • matrix[r][c] is "0" or "1"