"One Day together Leetcode" #85. Maximal Rectangle

Source: Internet
Author: User

One Day together Leetcode

This series of articles has all been uploaded to my github address: Zeecoder ' s GitHub
You are welcome to follow my Sina Weibo, my Sina Weibo blog
Welcome reprint, Reprint please indicate the source

(i) Title

Given a 2D binary matrix filled with 0 's and 1 ' s, find the largest rectangle containing all ones and return to its area.

(ii) Problem solving

Topic: Given a binary matrix, the maximum area of all sub-matrices containing 1 is calculated in the matrix.
For example:
1000
1101
1111
1111
The formed rectangle has a No. 0 column area of 4, 2nd, 3 row area 8 and so on.
So let's think about how to find these rectangles and figure out their area.
for (I,J) we have it as the upper left corner of the rectangle, then need to find out how many 1 it extends to the right, how many 1 it extends down, and how many 1 of each line extends to the right, such as (1,0) extends right 2 1, down 3 1, then (2,0), (3,0) to the right extends greater than 2, So the area of this rectangle is 6.

classSolution { Public:intMaximalrectangle ( vector<vector<char>>& Matrix) {if(Matrix.empty ())return 0;introw = Matrix.size ();intCol = matrix[0].size (); vector<vector<int>>Samerownum;//Store The current point down how many 1         vector<vector<int>>Samecolnum;//Store current point extend to right how many 1         for(inti =0; i < row; i++) { vector<int>TMP (COL,0);            Samecolnum.push_back (TMP);        Samerownum.push_back (TMP); } for(inti =0; i < row; i++)//Calculation Samerownum{ for(intj =0; J < Col; )            {if(Matrix[i][j] = =' 1 ') {intt = j;intCount =0; while(T < col&&matrix[i][t++] = =' 1 ') count++;//calculation of how many 1                     while(j<t) samecolnum[i][j++] = count--;//Assigned value}Elsej + +; }        } for(inti =0; I < col; i++) { for(intj =0; J < Row; )            {if(Matrix[j][i] = =' 1 ') {intt = j;intCount =0; while(T < row&&matrix[t++][i] = =' 1 ') count++;//calculation of how many 1                     while(j<t) Samerownum[j++][i] = count--;//Assigned value}Elsej + +; }        }intMaxarea =0; for(inti =0; i < row; i++) { for(intj =0; J < col;j++) {if(matrix[i][j]==' 1 ')                {intt = i;intMincol = Samecolnum[i][j]; while(T < row&&t < i + samerownum[i][j]) {Mincol = Mincol < Samecolnum[t][j]? Mincol:samecolnum[t][j];//Calculate the rectangular area in turn                        intArea = mincol* (t-i+1); Maxarea = max (Maxarea, area);//Seek maximum valuet++; }                }            }        }returnMaxarea; }};

"One Day together Leetcode" #85. Maximal Rectangle

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.