[Leetcode] 85. Maximal Rectangle Java

Source: Internet
Author: User

Topic:

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

For example, given the following matrix:

1 1 1 1 1 11 0 0 1 0

Return 6.

test instructions and analysis: is to give a matrix, find a whole is one of the largest sub-matrix. a two-dimensional matrix containing 0 and 1 is given, which requires that the maximum sub-matrix containing only 1 is obtained. Determine if it is possible to form an all-1 rectangle at the point of the matrix[i][j] and the top left corner: If the current point is 0, it is certainly not, if the current point is not 0, then how to find the top left corner can be obtained by all 1 of the largest matrix, here can be converted to the maximum area of the histogram.

Code:

classSolution { Public intMaximalrectangle (Char[] matrix) {//a two-dimensional matrix containing 0 and 1 is given, which requires that the maximum sub-matrix containing only 1 is obtained. Determine if it is possible to form an all-1 rectangle at the point of Matrix[i][j] and the upper-left corner: If the current point is 0, it is certainly not, and if the current point is not 0,        intXlength =matrix.length; if(xlength==0)return0; intYlength = matrix[0].length; int[] Tempmatrix =New int[Xlength] [Ylength];//Tempmatrix[i][j] Represents the number of consecutive 1 in the first row of column J         for(intj=0;j<ylength;j++) {tempmatrix[0][J] = (matrix[0][j]== ' 0 '? 0:1); }         for(intj=0;j<ylength;j++) {             for(inti=1;i<xlength;i++){                if(matrix[i][j]== ' 0 ') Tempmatrix[i][j]= 0; Else{Tempmatrix[i][j]= Tempmatrix[i-1][j]+1; }            }        }        intMax = 0;  for(inti=0;i<xlength;i++){            inttemp =Largestrectanglearea (Tempmatrix[i]); if(Max <temp) Max=temp; }        returnMax; }    Private intLargestrectanglearea (int[] Heights) {//What should I do if the height array is known to be ascending? For example 1,2,5,7,8 (1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1) is Max (height[i]*), and the purpose of using stacks is to construct such ascending sequences, which are solved by the above method. Save the maximum possible value when constructing the stack        if(Heights = =NULL)            return0; intTempresult = 0; Stack<Integer> stack =NewStack<>(); Stack.push (heights[0]);  for(inti=1;i){            if(Heights[i]>=stack.peek ()) {//AscendingStack.push (Heights[i]); }Else{                if(!Stack.isempty ()) {                    intCount = 0; intMin =Stack.peek ();  while(!stack.isempty () && Stack.peek () >Heights[i]) {                        if(Stack.peek () <min) {min=Stack.peek ();                        } stack.pop (); Count++; if(tempresult<count*min) {Tempresult= count*min; }                    }                    intJ=0;  while(j<=count)                        {Stack.push (heights[i]); J++; }                }            }        }         for(inti=heights.length-1;i>=0;i--){            intx=Stack.pop (); if((heights.length-i) *x>Tempresult) {Tempresult= (heights.length-i) *x; }        }        returnTempresult; }}

[Leetcode] 85. Maximal Rectangle Java

Related Article

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.