目录
1.Problem description
description:
given an m * n binary matrix filled with only 0 and 1,find the largest square containing only
1 and return its area.
here is an example:
in the picture above, the largest square containing only 1 has been circled out, so the output should be 2*2=4.
constraints:
1> m represents the size of matrix
2> n represents the size of matirx[i]
3>m and n meet the condition:1<=m,n<=300
4>every element in the matrix is either 1 or 0
2.Concrete analysis
method selection:
the easiest way to find out the largest square that meet the criteria of the question is to iterate through all possible squares, but it's clear that this is also the most violent and bulkiest way because once the test data is too large the memory will overflow.Dealing with questions connected with binary matrix, the most suitable way is to use "dynamic programming".
detailed steps:
according to the steps of dynamic programming, first we need to define the meaning of elements in the array. For this question, we have to create a binary array to record data.The key to this problem is what every element in the array meas.The question requires us to find the largest square,so for a element with a subscript [i][j],there is a common mistake that is to directly define this element as the largest square in the range from [1][1] to [i][j] which makes the question even tougher.The correct solution is to define the element as the largest square with it as the lower-right corner.
Then we need to find the ties between every element.If in the matrix there is a '0',it means that no square will take it as the lower-right corner because the target matrix can only contain '1'.When there is a '1' in the matrix,we need to start looking from the elements on the top ,left and upper left corrner.We assume that the minimum value of these three points is x,which means that they can all be a lower-right corner of a square with a side length of x.Now we paint these three square,and if we take the element as the lower-right corner,it was clear that it can be a lower-right corner of a square with a side length of x+1.
so now we have figured out their connections:
value[i][j]=min(min(value[i-1][j],value[i][j-1]),value[i-1][j-1])+1
Last step is to find the initial value.Only after we find all the initial values can we continue to calculate the final result.In the binary array we create,we calculate a element value through the element on the top,left and upper left.Thus,for the first line and first column,we need to initialize all the elements to '1' or '0',which only depends on the corresponding elements in the matrix given.
Finally,we obtain data for each point .All we need to do now is to find the largest one and calculate its area.
3.Code implementation
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m=matrix.size();
int n=matrix[0].size();
if(m==0||n==0)
return 0;
int ans=0;
int dp[m][n];
for(int i=0;i<m;i++)
{
if(matrix[i][0]=='1')
dp[i][0]=1;
else
dp[i][0]=0;
ans=max(ans,dp[i][0]);
}
//
for(int j=0;j<n;j++)
{
if(matrix[0][j]=='1')
dp[0][j]=1;
else
dp[0][j]=0;
ans=max(ans,dp[0][j]);
}
//
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
if(matrix[i][j]=='0')
dp[i][j]=0;
else
dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
ans=max(ans,dp[i][j]);
}
}
return ans*ans;
}
};
4.Summary
The key to this question is dynamic programming and the hardest part of it is to figure out the connections between points.Coping with those binary matrix,in most of the time we ask dynamic programming for help.As for looking for the connections,we can brush up on this type of problems to improve proficiency.