lintcode-436-Maximal Square

 
 
class Solution {
public:
    /**
     * @param matrix: a matrix of 0 and 1
     * @return: an integer
     */
    int min(int a,int b,int c){
        a=a<b?a:b;
        return a<c?a:c;
    }
    int maxSquare(vector<vector<int> > &matrix) {
        // write your code here
        if(matrix.empty()) 
            return 0; 
          
        int n = matrix.size();  
        int m = matrix[0].size();  
          
        int dp[n][m];  
        int max = 0;
        
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; ++i) {  
            if(matrix[i][0]==1) {  
                dp[i][0] = 1;  
                max = 1;  
            }  
        }  
          
        for(int j=0; j<m; ++j) {  
            if(matrix[0][j]==1) {  
                dp[0][j] = 1;  
                 max = 1;  
            }  
        }  
        for(int i=1; i<n; ++i) {  
            for(int j=1; j<m; ++j){  
                if(matrix[i][j]==0){
                    dp[i][j]=0;  
                }else {  
                    dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]) + 1;  
                    if(max<dp[i][j])
                        max=dp[i][j];
                }  
            }  
        }  
        return max*max;
    }
};

你可能感兴趣的:(lintcode-436-Maximal Square)