221. Maximal Square

LeetCode

DP: dp(i,j)=min(dp(i−1,j),dp(i−1,j−1),dp(i,j−1))+1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length==0) return 0;

int rowNum=matrix.length;
int colNum=matrix[0].length;
int[][] dp=new int[rowNum+1][colNum+1];
int maxEdgeLen=0;

// i: length of row; j: length of colomn.
for(int i=1;i<=rowNum;i++){
for(int j=1;j<=colNum;j++){
if(matrix[i-1][j-1]=='1'){
dp[i][j]=Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
maxEdgeLen=Math.max(dp[i][j],maxEdgeLen);
}
}
}
return maxEdgeLen*maxEdgeLen;
}
}

0%