"Dynamic planning" leetcode-maximal Square

Source: Internet
Author: User

Title:

Maximal Square 

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:

Solve with dynamic programming. Create a class node. The member variable left in node has a number of 1 (containing the point itself), a few 1 (containing the point itself), and a side length (the point in the lower right corner of the square) of the corresponding maximum square of the maxsize. If a point is ' 0 ', then its corresponding node is (0,0,0).

1. Use variable res to record the edge length of the largest square.

2, first of all, the input matrix is processed in the upper left corner, the first row and the first column, to find out the node value of these locations.

3. Go through the rest of the matrix in turn and find the node value for each point. and update Res.

4, return to Res*res.


Class node{Public:int left,up,maxsize; Node (): Left (0), up (0), maxsize (0) {} node (Int. a,int b,int C): Left (a), up (b), maxsize (c) {}};class Solution {Public:int maxi  Malsquare (vector<vector<char>>& matrix) {if (Matrix.empty () | | matrix[0].empty ()) return                    0;        int rows=matrix.size (), cols=matrix[0].size ();        int res=0;                Vector<vector<node>> DP (rows,vector<node> (COLS));            if (matrix[0][0]== ' 1 ') {Res=1;        Dp[0][0]=node (1,1,1);                } for (int j=1;j<cols;++j) {if (matrix[0][j]== ' 1 ') {Res=1;            Dp[0][j]=node (dp[0][j-1].left+1,1,1);                }} for (int i=1;i<rows;++i) {if (matrix[i][0]== ' 1 ') {Res=1;            Dp[i][0]=node (1,dp[i-1][0].up+1,1); }} for (int i=1;i<rows;++i) {T j=1;j<cols;++j) {if (matrix[i][j]== ' 1 ') {DP[I][J].L                     eft=dp[i][j-1].left+1;                     dp[i][j].up=dp[i-1][j].up+1;                     if (matrix[i-1][j-1]!= ' 1 ') dp[i][j].maxsize=1;                         else {int tmp=min (dp[i-1][j-1].maxsize+1,dp[i][j].left);                          Tmp=min (tmp,dp[i][j].up);                     dp[i][j].maxsize=tmp;                  } res=max (Res,dp[i][j].maxsize);    }}} return res*res; }};



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

"Dynamic planning" 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.