-
Notifications
You must be signed in to change notification settings - Fork 0
221_MaximalSquare
a920604a edited this page Apr 14, 2023
·
1 revision
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
vector<vector<int>> dp (n, vector<int>(m,0));
int ret = 0;
for(int i =0;i<n;++i){
for(int j=0;j<m;++j){
if(matrix[i][j] == '0') continue;
else if(i== 0 || j==0) dp[i][j] = matrix[i][j] - '0';
else dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+1;
ret = max(ret, dp[i][j]);
}
}
return ret*ret;
}
};
- time complexity
O(n*n)
- space complexity
O(n*n)
footer