* leetcode #85 in cpp

Solution: 

This question is hard. I did not know how to solve this until I looked up the solution online. Basically to solve this question we employ techniques in #84. That is, we treat this problem as looking for largest rectangle in histogram. 

Example:

given 

011
111
101
We treat each row as the x-axis, and the 1s as the height of the histogram.  

For row 1:  it is [0, 1,1], max rectangle in this histogram is 2.

For row 2: it is [1,2,2], max rect in this histogram is 4; 

For row 3: it is [2,0,3], max rect in this histogram is 3;

Thus we first need to transform the matrix into histograms based on each row(step1), then we use #84 to find out the max rect in the histograms. 

Code:

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty()) return 0;
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> hist(m, vector<int>(n,0));
<span style="white-space:pre">	</span>// step 1
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(matrix[i][j] == '1'){
                    if(i-1>=0) hist[i][j] = hist[i-1][j] + 1;
                    else hist[i][j] = 1;
                }
            }
        }
<span style="white-space:pre">	</span>
        int max_area = 0;
        for(int i = 0; i < m; i ++){
            max_area = max(max_area,largestRectangleArea(hist[i]));
        }
        return max_area;
    }
    int largestRectangleArea(vector<int>& heights) {
        heights.push_back(0);
        stack<int> left_ind;
        int max_area = 0;
        int i = 0;
        while(i < heights.size()){
            if(left_ind.empty()) {
                left_ind.push(i);
                i++;
            }
            else{
                if(heights[i] > heights[left_ind.top()]){
                    left_ind.push(i);
                    i++;
                }else{
                    int left = left_ind.top();
                    left_ind.pop();
                    max_area = max(max_area, heights[left] *(left_ind.empty()?  i  :(i - left_ind.top() - 1)));
                }
            }
            
            
        }
        return max_area; 
    }

        
        
    
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值