Skip to content

Commit fcffe3f

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 221_Maximal_Square.java
1 parent 1ca6f7b commit fcffe3f

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Dynamic Programming/221_Maximal_Square.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ public int maximalSquare(char[][] matrix) {
44
return 0;
55
}
66

7-
int m = matrix.length, n = matrix[0].length;
8-
int[][] dp = new int[m + 1][n + 1];
9-
int result = 0;
7+
int maxSquareLen = 0;
8+
int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
109

11-
for (int i = 1; i <= m; i++) {
12-
for (int j = 1; j <= n; j++) {
10+
for (int i = 1; i <= matrix.length; i++) {
11+
for (int j = 1; j <= matrix[0].length; j++) {
1312
if (matrix[i - 1][j - 1] == '1') {
14-
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
15-
result = Math.max(result, dp[i][j]);
13+
dp[i][j] = 1 + Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]);
14+
maxSquareLen = Math.max(maxSquareLen, dp[i][j]);
1615
}
1716
}
1817
}
1918

20-
return result * result;
19+
return maxSquareLen * maxSquareLen;
2120
}
2221
}

0 commit comments

Comments
 (0)