Leetcode first _ Maximal Rectangle

Source: Internet
Author: User

This question is more difficult than it was just now. If you have never done the last one, it is heartless.

First think of a stupid solution, how to determine a rectangle? Find a top left corner and check the position where each row can be extended. Note that as the number of rows increases, the number of columns can only be shortened and cannot be extended. I want to know the complexity of this method, and the timeout is no doubt.

Will it be inspired if I just did this question of seeking for a column. Every 1 in the matrix is regarded as a small square. In each column, continuous 1 forms a column, the maximum area enclosed by a series of such columns is the largest rectangle of all 1, and the problem is perfectly transformed. Although in my opinion, this kind of conversion is very difficult. If these two questions are not adjacent, it is too difficult to think about them. This gives us a good lesson. There may be some relationship between different shapes.

How to construct such a column is much simpler, and each row needs to be calculated. If the row is 1 or 0 at the top of the current row and 1 at the top of the row, the column length increases by 1 on the top of the corresponding column, if it is 0, it is 0. Returns the largest area.

class Solution {public:    int largestArea(int *height, int length){        stack
 
   s;        int i=0, res=0, tpres, tph;        while(i
  
    > &matrix) {        int m = matrix.size();        if(m == 0)  return 0;        int n = matrix[0].size();        int **h = new int*[m];        for(int i=0;i
   
    



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.