Maximal Square

code


class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if(matrix.size() == 0)
        {
            return 0;
        }

        int m = matrix.size();
        int n = matrix[0].size();

        vector<vector<int>> dp(m, vector<int>(n, 0));

        int ret = 0;

        for(int i = 0; i<m; i++)
        {
            if(matrix[i][0] == '1')
            {
                dp[i][0] = 1;
                ret = 1;
            }
        }

        for(int i = 0; i<n; i++)
        {
            if(matrix[0][i] == '1')
            {
                dp[0][i] = 1;
                ret = 1;
            }
        }

        for(int i = 1; i<m; i++)
        {
            for(int j = 1; j<n; j++)
            {
                if(matrix[i][j] == '0')
                {
                    continue;
                }
                dp[i][j] = min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1])) + 1;
                ret = max(ret, dp[i][j]);
            }
        }

        return ret*ret;
    }
};

results matching ""

    No results matching ""