Leetcode: maximal square(221)

本文介绍了解决给定二进制矩阵中找到包含全1的最大正方形面积问题的动态编程方法。关键步骤包括定义数组记录数据,找出元素间的关联,以及初始化和计算最大值。代码实现展示了如何使用动态规划求解此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.Problem description

2.Concrete analysis

3.Code implementation


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. 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值