Leetcode--Maximal Square

Source: Internet
Author: User

Question:

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.

Analysis:

Give a 2D binary array to find the maximum area of the square that is composed entirely of 1.

Ideas:

Each time the current element (I, j) is used as the lower-right or upper-left corner of a square. For example, if it is the lower-right corner, the position of the current element is 1: dp[i][j] = min (dp[i-1][j-1] dp[i-1][j], dp[i][j-1]) + 1; (The edge length is the smallest edge length in four weeks plus the contribution of the current element). At first, it was always felt that this implementation would only find a square with a side length of 2, which could not be obtained when the side length was 3 or more. But it's not. Draw a picture of yourself and walk through the whole process to understand.

Therefore, the most important part of dynamic programming is to obtain initial conditions and state transfer equations.

For the subject: the initial condition is the first row of the first column is the original element (the edge length can only be 0 or 1);

The state transition equation is: dp[i][j] = min (dp[i-1][j-1] dp[i-1][j], dp[i][j-1]) + 1;

Therefore, the code is as follows:

 Public classSolution { Public intGetmin (intAintBintc) {intt =Math.min (A, b); intR =math.min (t, c); returnR; }     Public intMaximalsquare (Char[] matrix) {        if(Matrix = =NULL|| Matrix.length = = 0 | | Matrix[0].length = = 0)            return0; introw = matrix.length, col = matrix[0].length; int[] DP =New int[Row][col]; intMax = 0;  for(intj=0; j<col; J + +) {            if(Matrix[0][j] = = ' 1 ') {dp[0][J] = 1; Max= 1; }        }         for(inti=0; i<row; i++) {            if(matrix[i][0] = = ' 1 ') {dp[i][0] = 1; Max= 1; }        }         for(intI=1; i<row; i++) {             for(intJ=1; j<col; J + +) {                if(Matrix[i][j] = = ' 1 ') Dp[i][j]= Getmin (Dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1; ElseDp[i][j]= 0; Max=Math.max (Max, dp[i][j]); }        }        returnMax *Max; }}

Small summary: In fact, do so many dynamic planning problems, the total body size on the beginning of the summary of the three types of questions Http://www.cnblogs.com/little-YTMM/p/5372680.html:LIS, knapsack problem, checkerboard problem. By adapting these three sizes, you can solve most of the DP problems by learning to apply them.

Leetcode--Maximal Square

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.