-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSolution221.java
More file actions
29 lines (27 loc) · 1.01 KB
/
Solution221.java
File metadata and controls
29 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package dynamic_problem;
/**
* 最大正方形:可以用动态规划,以某点为右下角的最大正方形边长 只与
* 以该点上面、左边、左上相邻点为右下角的最大正方形边长有关,
* 取最小+1的关系。用二维数组储存结果需要补充上边和左边的
* 数组 2d dp,也可以用一位数组储存结果,更节约空间 1d dp。
*/
public class Solution221 {
public int maximalSquare(char[][] matrix) {
int row = matrix.length;
int col = matrix.length==0? 0:matrix[0].length;
int[] dp = new int[col+1];
int ans = 0;
for (int i = 1; i < row+1; i++){
int prev = 0;
for (int j = 1; j < col+1; j++){
int temp = dp[j];
if (matrix[i-1][j-1]=='1'){
dp[j] = Math.min(Math.min(dp[j-1],dp[j]),prev)+1;
ans = Math.max(ans,dp[j]);
} else {dp[j] = 0;}
prev = temp;
}
}
return ans*ans;
}
}