-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMaximalSquare.java
More file actions
30 lines (27 loc) · 1.01 KB
/
MaximalSquare.java
File metadata and controls
30 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
30
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0
|| matrix[0].length == 0) return 0;
int[][] dp = new int[matrix.length][matrix[0].length];
int max = 0;
for (int i = 0; i < matrix.length; i++) {
dp[i][0] = Character.getNumericValue(matrix[i][0]);
max = Math.max(max, dp[i][0]);
}
for (int i = 0; i < matrix[0].length; i++) {
dp[0][i] = Character.getNumericValue(matrix[0][i]);
max = Math.max(max, dp[0][i]);
}
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[i][j] == '0') {
dp[i][j] = 0;
} else {
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
max = Math.max(max, dp[i][j]);
}
}
}
return max * max;
}
}