Given a 2D binary matrix filled with 0 's and 1 ' s, find the largest square containing all 1 's and return its area.
For example, given the following matrix:
1 1 1 1 11 0 0 1 0
Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
Find the area of a matrix that contains 1 of the largest squares.
Solution One
The core of dynamic planning is the definition of state and state transition equations:
Set A[i][j] represents the edge length of a square with all 1 at the coordinates of the subscript (I,J).
Then the matrix of the matrix corresponding to the above example is:
[1 0 1 0 0]
[1 0 1 1 1]
[1 1 1 2 1]
[1 0 0 1 0]
It is not difficult to find a[i][j] and a[i-1][j-1] and the line I and J column.
A[I][J] is related to the number of consecutive 0 elements in the line I (I,J) beginning with a[i-1][j-1] and the minimum number of consecutive 0 from the beginning of (I,J) the first column J A[i-1][j-1] elements. such as:
This method of analysis is not very good, because the time complexity is O (m*n*min (m,n)), the spatial complexity is O (m*n). M is the length of the matrix, and n is the width of the matrix.
Runtime:16ms
Class Solution {Public:int maximalsquare (vector<vector<char>>& matrix) {int height=matrix.size (); if (height==0) return 0; int width=matrix[0].size (); Vector<vector<int>> Vec (height,vector<int> (width,0)); int result=0; for (int i=0;i
Solution Two
And then see discuss found above the innermost loop actually beg of a little wronged, because A[i][j] is to (I,J) for the bottom right corner of the largest square edge length, so do not need to use for loop, at this time the state transition equation is significantly more, not like the solution 1 is a little blurred, Can even be described in words.
If MATRIX[I][J] is 1, then A[i][j]=min (a[i-1][j-1],a[i-1][j],a[i][j-1]) +1; if Matrix[i][j] is 0, then A[i][j] is 0.
Looking at this equation is a lot more understandable.
Such as:
The largest square side length is max{a[i][j]}.
At this point the time complexity is O (m*n), the space complexity is O (m*n).
Runtime:14ms
int Maximalsquare (vector<vector<char>>& matrix) { int height=matrix.size (); if (height==0) return 0; int width=matrix[0].size (); Vector<vector<int>> Vec (height,vector<int> (width,0)); int result=0; for (int i=0;i
Solution Three
Then we found a way to improve the spatial complexity:
From the above solution two can be seen A[i][j] only with a[i-1][j],a[i][j-1],a[i-1][j-1], that is, only with the current column and the previous column, so you can only use a vector containing two of the length of the matrix to save the data. This time complexity is O (m*n), the spatial complexity is O (N).
Runtime:15ms
int Maximalsquare (vector<vector<char>>& matrix) { int height=matrix.size (); if (height==0) return 0; int width=matrix[0].size (); Vector<vector<int>> Vec (2,vector<int> (width,0)); int result=0; for (int i=0;i
A reference solution in Leetcode: https://leetcode.com/discuss/39403/simple-and-efficient-implementation-using-dp-in-c
Https://leetcode.com/discuss/38489/easy-solution-with-detailed-explanations-8ms-time-and-space
Leetcode221:maximal Square