Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

221. Maximal Square

Posted on 2020-06-09 | Edited on 2021-01-22

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;
}
}

<1…178179180…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%