Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2053 from user54778/main
Browse files Browse the repository at this point in the history
Create 0221-maximal-square.java Bottom Up tabulation
  • Loading branch information
tahsintunan authored Jan 18, 2023
2 parents c69e03e + 8df8584 commit 0964661
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/0221-maximal-square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
// dp bottom up
public int maximalSquare(char[][] matrix) {
int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
int maxLen = 0;
for (int row = matrix.length - 1; row >= 0; row--) {
for (int col = matrix[0].length - 1; col >= 0; col--) {
if (matrix[row][col] == '1') {
dp[row][col] = 1 + Math.min(Math.min(dp[row + 1][col], dp[row][col + 1]), dp[row + 1][col + 1]);
maxLen = Math.max(maxLen, dp[row][col]);
}
}
}

return maxLen * maxLen;
}
}

0 comments on commit 0964661

Please sign in to comment.