Leetcode: maximal rectangle

Source: Internet
Author: User

Leetcode: maximal rectangle

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

Address: https://oj.leetcode.com/problems/maximal-rectangle/

Algorithm: To solve this problem, use largest rectangle in histogram. We regard each row of the matrix as the bottom of the histogram's bar. For example, row I can be seen as the histogram between the last row and row I, in addition, the height of each column is the number of rows that are consecutively set to 1 from row I on the column. In this way, the maximum matrix value of the last edge in the I behavior matrix is the maximum matrix area in the histogram. That is to say, for no row, we can calculate the histogram represented by this row, and then call the function in largest rectangle in histogram to find the largest matrix above this behavior, find the Maximum Matrix of the entire problem from all rows. Code:

 1 class Solution { 2 public: 3     int maximalRectangle(vector<vector<char> > &matrix) { 4         if(matrix.empty() || matrix[0].empty()) return 0; 5         int row_num = matrix.size(); 6         int col_num = matrix[0].size(); 7         vector<int> histogram1(col_num), histogram2(col_num); 8         for(int j = 0; j < col_num; ++j){ 9             histogram1[j] = matrix[row_num-1][j] - ‘0‘;10         }11         int max_area = largestRectangleArea(histogram1);12         vector<int> *p_now = &histogram2;13         vector<int> *p_pre = &histogram1;14         for(int i = row_num-2; i >= 0; --i){15             for(int j = 0; j < col_num; ++j){16                 if(matrix[i][j] == ‘0‘){17                     (*p_now)[j] = 0;18                 }else if(matrix[i+1][j] == matrix[i][j]){19                     (*p_now)[j] = (*p_pre)[j] + 1;20                 }else{21                     (*p_now)[j] = 1;22                 }23             }24             int area = largestRectangleArea(*p_now);25             if(area > max_area)26                 max_area = area;27             vector<int> *temp = p_now;28             p_now = p_pre;29             p_pre = temp;30         }31         return max_area;32     }33     int largestRectangleArea(vector<int> &height) {34         int len = height.size();35         if(len < 1) return 0;36         stack<int> stk;37         int i = 0;38         int max_area = 0;39         while(i < len){40             if(stk.empty() || height[stk.top()] <= height[i]){41                 stk.push(i++);42             }else{43                 int t = stk.top();44                 stk.pop();45                 int area = height[t] * (stk.empty() ? i : i - stk.top() - 1);46                 if(area > max_area){47                     max_area = area;48                 }49             }50         }51         while(!stk.empty()){52             int t = stk.top();53             stk.pop();54             int area = height[t] * (stk.empty() ? len : len - stk.top() - 1);55             if(area > max_area){56                 max_area = area;57             }58         }59         return max_area;60     }61 };

 

Leetcode: 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.