Maximal Square
Problem Statement
Given an m x n
binary matrix filled with 0's
and 1's
, find the largest square containing only 1's and return its area.
Example 1:
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 4
Example 2:
Input: matrix = [["0","1"],["1","0"]]
Output: 1
Example 3:
Input: matrix = [["0"]]
Output: 0
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 300
matrix[i][j]
is '0
' or '1
'.
Code
Python Code
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if matrix is None or len(matrix) < 1:
return 0
rows = len(matrix)
cols = len(matrix[0])
dp = [[0]*(cols+1) for _ in range(rows+1)]
max_side = 0
for r in range(rows):
for c in range(cols):
if matrix[r][c] == '1':
dp[r+1][c+1] = min(dp[r][c], dp[r+1][c], dp[r][c+1]) + 1 # Be careful of the indexing since dp grid has additional row and column
max_side = max(max_side, dp[r+1][c+1])
return max_side * max_side