221. Maximal Square
Last updated
Was this helpful?
Last updated
Was this helpful?
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
For example, given the following matrix:
Return 4.
Thoughts:
different approach from maximal rectangle problems (and )since it is a square (current state value only depends on top, left and top-left corner
f[i][j] number of maximal square from matrix[0, ...i -1][0,...j-1] so far
initial state: f[i][0] = f[0][j] = 0
recursive state : f[i][j] = min(f[i-1][j], f[i][j-1], f[i-1][j-1]) for (1<= i <= matrix.size(); 1<=j<=matrix[0].size()).
further optimization
Code Time Complexity O(row * col), Space Complexity O(row * col)
Code (Optimization): with Space Complexity O(row) or O(col)
Special Thanks to 's