# leetcode Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 4.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

dp，递归关系式:dp[x][y]=min(dp[x-1][y],dp[x][y-1],dp[x-1][y-1])+1，dp[x][y]代表以x，y为右下顶点的正方形的最长边长，代码：

public int maximalSquare(char[][] matrix) {
int length=0;
int[][] res=new int[matrix.length+1][matrix[0].length+1];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]=='1'){
res[i+1][j+1]=Math.min(Math.min(res[i][j+1],res[i+1][j]),res[i][j])+1;
if(res[i+1][j+1]>length) length=res[i+1][j+1];
}
}
}
return length*length;
}

#### leetcode 221: Maximal Square

2015-06-05 08:15:01

#### LeetCode221:Maximal Square

2015-06-18 22:53:12

#### LeetCode(221) Maximal Square

2015-08-10 21:53:25

#### LeetCode OJ 之 Maximal Square （最大的正方形）

2015-08-22 10:36:19

#### [leetcode] 221 Maximal Square(最大全1正方形 &动态规划)

2015-10-04 23:04:17

#### 221. Maximal Square | Java最短代码实现

2016-03-24 10:20:10

#### leetcode_question_85 Maximal Rectangle

2013-09-19 17:29:08

#### 算法学习之动态规划(leetcode 85. Maximal Rectangle）

2017-03-15 16:59:00

#### LeetCode85——Maximal Rectangle

2016-01-28 20:44:55

#### LeetCode --- 85. Maximal Rectangle

2015-04-03 17:09:12