其他分享
首页 > 其他分享> > 85. 最大矩形 Maximal Rectangle

85. 最大矩形 Maximal Rectangle

作者:互联网

题目 <https://leetcode-cn.com/problems/h-index/>

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize){
    if(matrixSize == 0 || matrixColSize[0] == 0){
        return 0;
    }

    int **dp;
    dp = malloc(sizeof(int *) * matrixSize);//dp[i][j]表示matrix[i][j]为结尾的行长度
    int i,j,k;
    for(i=0;i<matrixSize;i++){
        dp[i] = malloc(sizeof(int) * matrixColSize[i]);
    }
    int x,y;
    int area = 0,area1;
    for(i=0;i<matrixSize;i++){
        for(j=0;j<matrixColSize[i];j++){
            if(matrix[i][j] == '0'){
                dp[i][j] = 0;
                continue;
            }
            if(i-1>=0){
                dp[i][j] = dp[i-1][j]+1;
            }else{
                dp[i][j] = 1;
            }
            x=dp[i][j];
            y=0;
            for(k=j;k>=0&&matrix[i][k]=='1';k--){
                x = min(x,dp[i][k]);
                y++;
                area1 = x*y;
                area = max(area,area1);
            }
        }
        
    }
    for(i=0;i<matrixSize;i++){
        free(dp[i]);
    }
    free(dp);
    return area;
}

 

标签:Maximal,matrixSize,matrix,min,int,Rectangle,area1,dp,85
来源: https://blog.csdn.net/ZRXSLYG/article/details/111964332