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 0 1) 0 0
1 0 1) 1 1
1 1 1) 1 1
1 0 0) 1 0
Return 4.
1, define the so-called "state" that is what represents what: first I based on the existing information of the algorithm design technology is a two-dimensional dynamic programming is a two-dimensional table storage results; Convert the problem to a maximum of 1 array of edge length, because the result of the side length of the square is an area, a little redefinition of the problem Below I begin to study the structure of the feasible solution, the feasible solution is an array of all 1, the optimal solution is the maximum feasible solution of the edge length, the optimal value is the corresponding value of the optimal solution, an attribute edge length of the optimal solution; How do you think about scaling it down? ; To classify the solution in that piece; Considering the difference between the big problem and the small one, and the effect of this difference on the feasible solution, the optimal solution and the optimal value; After this series of thinking, define S[I,J] as the edge length of the largest full 1 array of rectangles in the upper left (i,j).
2, consider the so-called state transfer equation: S[i,j] depends on the s[i-1,j], depends on s[i,j-1], but also depends on the major problems and sub-problems, s[i,j] and S[i-1,j], s[i,j-1] The difference is the element (I,J); So how do you contribute the results of a sub-problem to the outcome of the problem? Define A[I,J] as the edge length of the maximum full 1 array containing the element (I,J), then get s[i][j] = max (s[i][j-1], s[i-1][j], a[i][j]);
3, we found that a[i][j] is not so easy to get, but more than s[i][j] easy to get, I expressed very happy, feel that everything has been on the track, two times dynamic planning: a[i][j] depends on what? ; by observing a two-dimensional array, a[i][j] relies on a[i-1][j-1]; slowly I find it only depends on a[i-1][j-1]? It also relies on the number of contiguous 1 rows from the element (I,J), the number of contiguous 1 of the column starting from the element (I,J), and we get the state transfer equation A[i][j] = min (a[i-1][j-1] + 1, tmprow, tmpcolumn);
4, the following work is: Fill in form A, and then use Table A to fill the table s
5, the end.
7, appendix-important manuscripts, recording the thinking process, as well as the guidance of discourse.
classSolution { Public:intMaximalsquare ( vector<vector<char>>& Matrix) {if(Matrix.empty ()) {return 0; }intLength = Matrix.size ();intwidth = matrix[0].size (); vector<vector<int>>A (length, vector<int>(width)); for(inti =0; I < width; i++) a[0][i] = matrix[0][i]-' 0 '; for(inti =0; i < length; i++) a[i][0] = matrix[i][0] -' 0 '; for(inti =1; i < length; i++) { for(intj =1; J < width; J + +) {intTmprow =0; while(Tmprow <= J && matrix[i][j-tmprow] = =' 1 ') tmprow++;intTmpcolumn =0; while(Tmpcolumn <= i && matrix[i-tmpcolumn][j] = =' 1 ') tmpcolumn++; A[i][j] = min (A[i-1][j-1] +1, Min (Tmprow, tmpcolumn)); } } vector<vector<int>>S (length, vector<int>(width)); s[0][0] = matrix[0][0] -' 0 '; for(inti =1; I < width; i++) s[0][i] = max (s[0][i-1], matrix[0][i]-' 0 '); for(inti =1; i < length; i++) s[i][0] = max (S[i-1][0], matrix[i][0] -' 0 '); for(inti =1; i < length; i++) { for(intj =1; J < width; J + +) {S[i][j] = max (max (S[i][j-1], S[i-1][J]), a[i][j]); } }returnS[length-1][width-1] * S[length-1][width-1]; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode (221) maximal Square