Skip to content

Latest commit

 

History

History
21 lines (20 loc) · 623 Bytes

221. Maximal Square.md

File metadata and controls

21 lines (20 loc) · 623 Bytes
class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int m = matrix.size(), n = matrix[0].size(), sz = 0;
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (!i || !j || matrix[i][j] == '0') {
                    dp[i][j] = matrix[i][j] - '0';
                } else {
                    dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
                }
                sz = max(sz, dp[i][j]);
            }
        }
        return sz * sz;
    }
};