Leetcode221 the largest square pass code and solution

Title description

In a two-dimensional matrix composed of 0 and 1, find the largest square containing only 1 and return its area.
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 1 0 0 1 0
Output: 4

Passed code

(1) My code

class Solution {
    
    
public:
    int maximalSquare(vector<vector<char>>& matrix) {
    
    
        int row=matrix.size();
        if(row==0)
        return 0;
        int col=matrix[0].size();
        int dp[row+10][col+10];
        int ans;
        int anss;
        int mid;
        int flag;
        ans=matrix[0][0]-'0';
        for(int i=0;i<row;i++)
        {
    
    
            dp[i][0]=matrix[i][0]-'0';
            if(dp[i][0]>ans)
            ans=dp[i][0];
        }
        for(int j=0;j<col;j++)
        {
    
    
            dp[0][j]=matrix[0][j]-'0';
            if(dp[0][j]>ans)
            ans=dp[0][j];
        }
        for(int i=1;i<row;i++)
        for(int j=1;j<col;j++)
        {
    
    
            mid=dp[i-1][j-1];
            dp[i][j]=matrix[i][j]-'0';
            if(mid!=0&&matrix[i][j]!='0')
            {
    
    
                for(int k=1;k<=mid;k++)
                {
    
    
                	flag=1;
                	for(int m=1;m<=k;m++)
                	{
    
    
                		if(matrix[i-m][j]=='0'||matrix[i][j-m]=='0')
                        {
    
    
                            flag=0;
                            break;
                        }
					}
					if(flag==1)
				    dp[i][j]=max(k+1,dp[i][j]);
                }
            }
            if(dp[i][j]>ans)
            ans=dp[i][j];
        }
        anss=ans*ans;
        return anss;
    }
};

(2) The best code online

class Solution {
    
    
public:
    int maximalSquare(vector<vector<char>>& matrix) {
    
    
        int row=matrix.size();
        if(row==0)
        return 0;
        int col=matrix[0].size();
        int dp[row+10][col+10];
        int ans=0;
        int anss;
        for(int i=0;i<=row;i++)
        for(int j=0;j<=col;j++)
        {
    
    
            dp[i][j]=0;
        }
        for(int i=1;i<=row;i++)
        for(int j=1;j<=col;j++)
        {
    
    
            if(matrix[i-1][j-1]!='0')
            {
    
    
                dp[i][j]=1+min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]));
                if(dp[i][j]>ans)
                ans=dp[i][j];
            }
        }
        anss=ans*ans;
        return anss;
    }
};

Problem solving ideas

When I first saw this problem, I thought it was solved by dynamic programming, but when I realized it, I didn't think about the recursion of dynamic programming. So if I did it according to my own ideas, I found that some of the test samples always failed because I still didn't consider it well. Finally, I finally passed all the samples and found that my code took a long time. So I searched for other people's solutions, and it turned out that there is a recursion, and I feel that the problem of dynamic programming can be solved very well as long as the recursion is clearly thought out. Let dp[i][j] represent the maximum square side length that can be formed by taking the i-th row and j-th column as the lower right corner, then the recurrence formula is: dp[i][j] = 1 + min(dp[i- 1][j-1], dp[i-1][j], dp[i][j-1]); In fact, every time you calculate the largest square with the current position as the lower right corner, you need to find the top, left , The smaller of the three upper left positions, because only in this way can the current largest square be obtained.
Every time I record the code I wrote and the best code on the Internet, I hope that I can quickly think of the recursive method when I brush up on this problem in the future and use the best method to solve it.

Guess you like

Origin blog.csdn.net/qq_38391210/article/details/108708391