Leetcode Daily Problem Solving ideas 221 maximal Square

Source: Internet
Author: User

Problem Description:

Title Link: 221 maximal Square

The problem to solve is to give a m*n matrix, only ' 1 ', ' 0 ', two elements, you need to find out from the ' 1 ' the largest square. Well, that's it.

We see that the label of this topic is DP, then the key to the problem is to find a recursive formula that matches whether the judgment is square.

The old routine, first look at the basic type, for a 2*2 square, for the lower right corner of the element (the first), his upper (0,1), left (1,0), the upper left (0,0) Three elements should be ' 1 ',

So as to be able to form a conforming square; then if it is a 3*3, first he must be a 2*2, then add a column to the left, add a row, by a 2*2 into a 3*3 type, so, for the Matrix in the

Any point ' 1 ' just keep matching his left column and the top row are ' 1 ', and count and then go outside to match a layer, know to appear ' 0 ' or reach the boundary, and then select the maximum number of layers to match from each point, done!

The basic routine is so, but every point to go once is m*n a point, after each point is again outside a layer by match, time complexity consumption, O (m^2*n^2), generally consider m*n, directly approximate 4 times the complexity of the square,

Certainly not advisable. Since there is no reliable way forward, then what about our backward push?

Removing the boundary (that is, the first row and the first column), for any one matrix[i][i], the square in which he is located should be determined by the previous layer: Left (matrix[i][j-1]), Upper (Matrix[i-1][j]), upper left (Matrix[i-1][j-1]),

Similarly, we also need a result matrix ret[m][n] to record the number of square layers at each corresponding point.

Look at the most basic type: For the point in the matrix[1][1], his left upper layer is all ' 1 ', so this point in ret[1][1] + 1, the result is recorded as 2,

That is: if: (Top! = ' 0 ', left!= ' 0 ', top_left!= ' 0 ')

THEN:RET[I][J] = matrix[i][j]+1;

In consideration of this step, feel OK, immediately write a good code on the page to submit, the result is a big wrong answer! Now feel right ah, for each point is not in the square has done

Judgment, and decided after the number of layers +1, so the output matrix one look:

  

Obviously, for the calculation of the layer, and do not get the previous layer of the square layer is in the number, you know, assuming that the [I][j] is a 3*3 square, then it must be a bit [i-1][j],[i][j-1], [i-1][j-1] These 3 are at least belong to

The square of the 2*2 may be clearer by illustration:

Left:top:left_top:

So the above scenario would look like this in a recursive-generated RET matrix:

Left:top:left_top:

Therefore, the corrected recursion should be:

  

Submit again, accepted!

Last-Resolved code:

  

In addition, there is a similar (link here), this indefinite shape, you are a row, or the square is not limited, the difficulty also promoted to the hard level,

But the core thought certainly did not change, everybody can try, this topic Ming Dogo katsuragi Paste personal solution idea.

Finally, the old adage, I put up the solution and thinking only as a record, or can also be crossing exchange learning, help is good.

Leetcode Daily Problem Solving ideas 221 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.