Leetcode--Maximal Square

Source: Internet
Author: User

Title Description:


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.




In a matrix, find the largest square area (1 is the point of the square, and 0 means empty).




The topic is a typical DP problem.


1. Assign a value of dp[0,i] to Matrix[0,i] and dp[i,0] to matrix[i,0]. I∈[0,n)


2. The two-layer loop is assigned a value of dp[i,j according to different conditions:
A. Matrix[i,j] = = 1 and 3 neighbors (dp[i-1,j],dp[i,j-1],dp[i-1,j-1]) are 1:dp[i,j] = 4
B. matrix[i,j] = = 1 and 3 neighbors >1 and equal: dp[i,j] = (square root of neighbor area +1) squared
C. matrix[i,j] = = 1 and 3 neighbors >=1 but not necessarily equal: dp[i,j]= (the square root of the smallest value in the neighbor +1) squared
D. Other circumstances: dp[i,j] = matrix[i,j]


3. Use the max variable to track the maximum value of the current DP[I,J]


Implementation code:



public class Solution {public int maximalsquare (char[,] matrix) {var row = Matrix.        GetLength (0); var col = Matrix.        GetLength (1); if (Row < 2) {if (row = = 0) {return 0;}        else if (col = = 1) {return matrix[0,0] = = ' 1 '? 1:0;}        } var max = 0;        var dp = new Int[row, col]; for (var i = 0;i < row; i++) {var x = matrix[i,0] = = ' 1 '? 1:0;dp[i, 0] = x;if (dp[i,0] > max) {max = Dp[i        , 0];} } for (var i = 0;i < col; i++) {var x = matrix[0,i] = = ' 1 '? 1:0;dp[0, I] = x;if (Dp[0,i] > max) {m        AX = dp[0,i];}                } for (var i = 1;i < row, i++) {for (var j = 1;j < col; J + +) {//Neighbours all equals 1 if (matrix[i,j] = = ' 1 ' && dp[i-1,j] = = 1 && dp[i, j-1] = = 1 && dp[i-1,j-1] = = 1) {if (dp[i                -1, j] = = 1) {Dp[i,j] = 4;} }//neighbours all bigger than 1 and equals each otherelse if (matrix[i,j] = = ' 1 ' && dp[i-1,J] = = Dp[i,j-1] && dp[i-1,j-1] = dp[i-1,j] && dp[i-1,j] > 1) {dp[i,j] = (int) Math.pow (math.sqrt (dp[i,j- 1]);} Neighbours all no less than 1, and may not equals each otherelse if (matrix[i,j] = = ' 1 ' && dp[i-1,j] >= 1 &am p;& Dp[i,j-1] >= 1 && dp[i-1,j-1] >= 1) {var min = math.min (Math.min (dp[i-1,j-1], dp[i-1,j]), dp[i,j-1]);                DP[I,J] = (int) Math.pow (math.sqrt (min) + +);}                else{Dp[i,j] = matrix[i,j] = = ' 1 '? 1:0;            }if (Dp[i,j] > max) {max = dp[i,j];}            }} return max; }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.