[Leetcode] Maximal Rectangle

Source: Internet
Author: User

Problem: For a matrix, the element of the matrix is '1' or '0', and the largest rectangle area consisting of all 1 is obtained. Analysis: before this question, we need to look at another question. Given an array, the elements in the array indicate the height of the rectangle. If each rectangle is of the same width, it is 1, find the maximum area that can be composed of this continuous rectangular sequence. For example:
The simplest half method is to find a continuous height higher than yourself for each height on the left and right, so that we can know the width, and the area with the height will be there, when such an operation is performed on each height, the overall maximum area is obtained, but the cost is 0 (N ^ 2 ). according to an old saying, "jian xiansiqi, the current saying is that it is better than your own nb, and will benefit you a lot. This is also the case. For the current height, it always wants to be higher than its own height, so its own width will increase, and the area determined by its own height will be larger. So what if I encounter something lower than my own height? In this case, you can either stand alone and work on your own. This may lead to a large area. Either keep a low profile, or join a team that is lower than yourself, you may also get more area than you do. For example, in section 6, you have to work on your own, with an area of 6, but drop down the body section. In cooperation with section 5, the area is 10. It seems that life philosophy is everywhere in life. We will sort out the above paragraph and summarize it as follows: 1) as long as the current processing height and the previous height values form an incremental sequence, we will continue. 2) The current height is smaller than the previous one. That is to say, the previous height will generate the two ideas we mentioned above. Are they themselves? Or are you working with others? So in implementation, we need to calculate both of these areas. So the area we create is a good computing. What if we work with others? For example, the height information of 5, 6, and 2, 6 can be passed to 5 through subscript. Why? Because 6 occupies a subscript after all, if there is no 6, the width from 2 to 5 is 1, and now the 6 width is 2. Because we use the stack-like structure when processing a given array of heights with "back" processing. So what is stored in the stack? Is the height saved? Obviously not, because the given parameter contains the height information. In order to obtain the area, we need the width information when we know the height. Because the width of each rectangle is 1, we need to know the number of rectangle bars, it is exactly what the subscript can provide for us. Therefore, the stack stores the base value corresponding to the height. The general idea is: 1) if the current stack is empty, or the current height value is greater than or equal to the stack height value, the stack is pushed to the stack. 2) if the current height value is less than the stack height value, so we need to exit the stack and calculate the area. Repeat 1. There is a problem here, for example, the height value is 1, 2, 3, 4, 5. all of them go into the stack, There is no out-of-stack operation, and there is no area calculation. Therefore, we need to add an extra sentry behind the height array, and the size is 0.
Now back to the original question, the original question is nothing more than a duplicate of the problem just now. The first line forms a maximum area, and the first two lines will also,..., until the first n rows. Then, the maximum values of these values are returned. The question must be a matrix composed of all. For the height of each row in the matrix, we have the following relationship: h [I] [j] = h [I-1] [j] + 1, if matix [I] [j] = 1, else h [I] [j] = 0.

Implementation:

 int RecArea(vector
 
  & heights){int len = heights.size();int maxArea = 0;//the index of heightvector
  
    hIndex;int i = 0;while(i < len){if(hIndex.size() == 0 || heights[i] >= heights[hIndex[hIndex.size() - 1]])hIndex.push_back(i++);//calculate the max area for now.else{int idex = hIndex[hIndex.size() - 1];hIndex.pop_back();int w = hIndex.size() == 0 ? i : i - hIndex[hIndex.size() - 1] - 1;int cur_area = heights[idex] * w;maxArea = max(maxArea, cur_area);}}return  maxArea;}int maximalRectangle(vector
   
     > &matrix) {int rows = matrix.size();if(rows == 0)return 0;int cols = matrix[0].size();int maxArea = 0;vector
    
      pre_row(cols + 1, 0);for (int row = 0; row < rows; ++row){vector
     
       cur_row(cols + 1, 0);for (int col = 0; col < cols; ++col){if(matrix[row][col] == '1')cur_row[col] = pre_row[col] + 1;elsecur_row[col] = 0;}int cur_area = RecArea(cur_row);maxArea = max(maxArea, cur_area);pre_row = cur_row;}return maxArea;}
     
    
   
  
 

int w = hIndex.size() == 0 ? i : i - hIndex[hIndex.size() - 1] - 1;

This statement: When the stack is empty, for example, in the first figure above, after processing 2, 1, 2 is obtained, the stack is output, and the stack is empty, at this time, I = 1. when the stack is not empty, for example, when the second height value is 2, the elements in the stack are 1, 5, 6 in sequence. Stack top is 6, get subscript, output stack, then stack top is 5, its subscript is 2, then I is 4.

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.