Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0964661

Browse files
authoredJan 18, 2023
Merge pull request #2053 from user54778/main
Create 0221-maximal-square.java Bottom Up tabulation
2 parents c69e03e + 8df8584 commit 0964661

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
 

Diff for: ‎java/0221-maximal-square.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
// dp bottom up
3+
public int maximalSquare(char[][] matrix) {
4+
int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
5+
int maxLen = 0;
6+
for (int row = matrix.length - 1; row >= 0; row--) {
7+
for (int col = matrix[0].length - 1; col >= 0; col--) {
8+
if (matrix[row][col] == '1') {
9+
dp[row][col] = 1 + Math.min(Math.min(dp[row + 1][col], dp[row][col + 1]), dp[row + 1][col + 1]);
10+
maxLen = Math.max(maxLen, dp[row][col]);
11+
}
12+
}
13+
}
14+
15+
return maxLen * maxLen;
16+
}
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.