221. Maximal Square 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718class 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; }}; analysis time complexity O(n*n) space complexity O(n*n)