Title Link: https://leetcode.com/problems/maximal-square/
Topic:
Given a 2D binary matrix filled with 0 's and 1 ' s, find the largest square containing all 1 's and return its area.
For example, given the following matrix:
1 1 1 1 11 0 0 1 0
Return 4.
Ideas:
With C[i][j] is expressed in a matrix of length I, Width j, containing matrix[i][j] this point of the largest square of the length.
Obviously C[i][j] is about the size of c[i-1][j], c[i][j-1], c[i-1][j-1]. The area surrounded by c[i-1][j], c[i][j-1], c[i-1][j-1].
C[i][j] = min (C[i-1][j], c[i][j-1], c[i-1][j-1]) +1
Algorithm:
public int maximalsquare (char[][] matrix) { if (matrix.length = = 0) return 0; int max = 0; int c[][] = new Int[matrix.length][matrix[0].length]; for (int i = 0, i < matrix.length; i++) {for (int j = 0; J < Matrix[0].length; J + +) { if (matrix[i][j] = =) 0 ') { continue; } if (i = = 0 | | j = 0) {//at Edge c[i][j] = 1; } else { c[i][j] = Math.min (C[i-1][j-1], math.min (c[i-1][ J], C[i][j-1])) + 1; } max = Math.max (max, c[i][j]); } } return Max * MAX; }
"Leetcode" maximal Square