其他分享
首页 > 其他分享> > 221.最大正方形

221.最大正方形

作者:互联网

### 解题思路

  1. dp[i][j]代表以位置(i,j)处的空格为正方形右下角的全为1的正方形的最大的边长。

  2. dp[i][j]=min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1,if (dp[i][j]==1);

  3. dp[i][j]=0,if (dp[i][j]==0);

  4. 边界,dp[i][0]=1 if dp[i][0]==1

  5. 边界,dp[0][i]=1 if dp[i][0]==1

  6. 暴力解法此处不再介绍,附上代码。

### 代码 d p dp dp解法,时间复杂度 O ( n 2 ) O(n^2) O(n2)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Solution
{
    const int MAXN = 1005;
    int lenx = 0;
    int leny = 0;
    int maxl = 0;
    //    int dp[MAXN][MAXN];
    int dp[205][205];
    int min_3(int a, int b, int c)
    {
        int res;
        if (a < b)
        {
            res = a;
        }
        else
        {
            res = b;
        }
        if (res < c)
        {
            return res;
        }
        return c;
    }

public:
    int maximalSquare(vector<vector<char>> &matrix)
    {
        lenx = matrix[0].size();
        leny = matrix.size();
        if (lenx == 0 || leny == 0)
        {
            return 0;
        }
        int max_xy = max(lenx, leny);

        // 初始化
        for (int i = 0; i < max_xy; i++)
        {
            for (int j = 0; j < max_xy; j++)
            {
                dp[i][j] = 0;
            }
        }

        // 下面开始计算
        for (int i = 0; i < leny; i++)
        {
            for (int j = 0; j < lenx; j++)
            {
                if (matrix[i][j] == '0')
                {
                    continue;
                }
                if (i == 0 || j == 0)
                {
                    dp[i][j] = 1;
                    continue;
                }
                dp[i][j] = min_3(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
            }
        }

        int res = dp[0][0];
        // 找到dp数组中的最小值
        for (int i = 0; i < leny; i++)
        {
            for (int j = 0; j < lenx; j++)
            {
                if (dp[i][j] > res)
                {
                    res = dp[i][j];
                }
            }
        }
        return res * res;
    }
};

int main()
{
    vector<vector<char>> test(4, vector<char>(5));
    int test_c[][5] = {{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}};
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            test[i][j] = test_c[i][j];
        }
    }
    Solution sol;
    cout << sol.maximalSquare(test) << endl;
    return 0;
}

### 代码暴力解法,时间复杂度 O ( n 3 ) O(n^3) O(n3)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Solution
{
    int lenx = 0;
    int leny = 0;
    int maxl = 0;

public:
    int maximalSquare(vector<vector<char>>& matrix)
    {
        lenx = matrix[0].size();
        leny = matrix.size();
        if (lenx == 0 || leny == 0)
        {
            return 0;
        }

        for (int j = 0; j < leny; j++) {
            for (int i = 0; i < lenx; i++) {
                if (matrix[j][i] == '0') {
                    continue;
                }
                int current_l = max(1, min(lenx - i, leny - j));
                int is_all_one = true;
                for (int k = j + current_l - 1, k1 = i + current_l - 1; k > j; k--, k1--) {
                    int is_all_one1 = true;
                    for (int p = 0; p < current_l; p++) {
                        // 扫描那一行和那一列
                        if (matrix[k][i + p] == '0' || matrix[j + p][k1] == '0') {
                            is_all_one1 = false;
                            break;
                        }
                    }
                    if (is_all_one1 == false) {
                        current_l = k - j;
                    }
                }
                maxl = max(current_l, maxl);
            }
        }
        return maxl * maxl;
    }
};

int main()
{
    vector<vector<char>>test(4, vector<char>(5));
    int test_c[][5] = { {'1','0','1','0','0'},{'1','0','1','1','1'},{'1','1','1','1','1'},{'1','0','0','1','0'} };
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 5; j++) {
            test[i][j] = test_c[i][j];
        }
    }
    Solution sol;
    cout << sol.maximalSquare(test) << endl;
    return 0;
}

标签:最大,int,++,正方形,221,leny,lenx,dp,matrix
来源: https://blog.csdn.net/shen253135371/article/details/113187077