Leetcode Maximal Rectangle-----java

Source: Internet
Author: User

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.

The largest sub-matrix area consisting of 1 in the matrix is evaluated.

1, first to observe, found that if the direct traversal and then find the maximum matrix of each point is obviously wrong, time complexity is too high.

2, combined with 84 questions, found that the two problems are very similar to the problem given in the example can be carefully observed can be found that the maximum sub-matrix is a line as the bottom, and then with the method of 84 questions to solve, so first to the matrix conversion, the matrix can be converted to the following:

1 0 1 0 0 1 0 1 1 11 1 1 1 1 1 0 0 1 0

After conversion:
1 0 1 0 02 0 2 1 13 1 3 2 24 0 0 3 0

The maximum matrix area is then solved for each row.

 Public classSolution { Public intMaximalrectangle (Char[] matrix) {        intLen1 =matrix.length; if(Len1 = = 0)            return0; intLen2 = matrix[0].length; if(Len2 = = 0)            return0; intresult = 0; int[] Heights =New int[Len1][len2];  for(inti = 0;i<len2;i++){            if(Matrix[0][i] = = ' 1 ') heights[0][i] = 1; }         for(inti = 1;i < len1;i++){             for(intj = 0;j<len2;j++){                if(Matrix[i][j] = = ' 1 ') Heights[i][j]= Heights[i-1][j]+1; }        }         for(inti = 0;i<len1;i++) {result=Math.max (Result,largestrectanglearea (heights[i)); }        returnresult; }     Public intLargestrectanglearea (int[] Heights) {        intLen =heights.length; intresult = 0; if(len = = 0)            return0; int[] left =New int[Len]; int[] right =New int[Len]; left[0] = 0;  for(inti = 1;i<len;i++){            intCurleft = i-1;  while(curleft >= 0 && heights[curleft]>=Heights[i]) {Curleft= Left[curleft]-1; } Left[i]= Curleft+1; } Right[len-1] = len-1;  for(inti = len-2;i>=0;i--){            intCurright = i+1;  while(Curright<len && heights[curright]>=Heights[i]) Curright= Right[curright]+1; Right[i]= CurRight-1; }         for(inti = 0;i<len;i++) {result= Math.max (result, (right[i]-left[i]+1) *(Heights[i])); }        returnresult;}}

Leetcode maximal Rectangle-----java

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.