https://leetcode.com/problems/maximal-square/description/
class Solution {
public int maximalSquare(char[][] matrix) {
int row = matrix.length;
if(row == 0) return 0;
int col = matrix[0].length;
int[][] dp = new int[row][col];
int sum = 0;
for(int i = 0; i < row; i++){
if(matrix[i][0] == '1'){
dp[i][0] = 1;
sum = Math.max(sum, dp[i][0]);
}else{
dp[i][0] = 0;
}
}
for(int i = 0; i < col; i++){
if(matrix[0][i] == '1'){
dp[0][i] = 1;
sum = Math.max(sum, dp[0][i]);
}else{
dp[0][i] = 0;
}
}
for(int i = 1; i < row; i++){
for(int j = 1; j < col; j++){
if(matrix[i][j] == '0')
dp[i][j] = 0;
else{
dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i-1][j]), dp[i][j-1])+1;
sum = Math.max(sum, dp[i][j]);
}
}
}
return (int)Math.pow(sum, 2);
}
}
网友评论