利用stack求柱狀圖的最大矩形面積

84Largest Rectangle in Histogram

問題描述:

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].


The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given heights = [2,1,5,6,2,3],
return 10.

問題解析:

1. 本題的意思是:給定一個arr,在arr中找出面積最大的矩形。
2. 最簡單的暴力求解是,相當於找出一個左邊界和右邊界,在計算兩個邊界之間高度最小的矩形面積。這種方法一定能夠找出最大面積,但是時間複雜度是O(n^2),不能AC。
3. 時間複雜度爲O(n)的解法。設置一個輔助棧sta,從arr第0個數開始,當arr[i]大於等於sta.top()時,就壓入棧,否則,說明sta.top()那個數的右邊界已經找到,則彈出sta.top(),計算當前面積cur,並與最大面積max比較大小或替換。彈出top時,一直彈出到當前的top值小於或者等於arr[i]爲止。並記錄剛纔彈出了幾個top,就再次壓入棧中幾個arr[i],最後壓入當前的arr[i]。

代碼如下:

class Solution {
public:
    // 利用堆棧來解決
    int largestRectangleArea(vector<int>& heights) 
    {
        if(heights.empty())
            return 0;
        stack<int>sta;
        int cur = 0, max = 0;
        int len = 0;
        int size = heights.size();
        for(int i=0; i<size; ++i)
        { 
            if(sta.empty() || sta.top() <= heights[i])
                sta.push(heights[i]);
            else
            {
                len = 0;
                while(!sta.empty() && sta.top() > heights[i])
                {
                    ++ len;
                    cur  = sta.top()*len;
                    if(cur > max)   max = cur;
                    sta.pop();
                }
                while(len)
                {
                    --len;
                    sta.push(heights[i]);
                }
                sta.push(heights[i]);
            }
        }
        while(!sta.empty())
        {
            ++ len;
            cur  = sta.top()*len;
            if(cur > max)   max = cur;
            sta.pop();
        }
        
        return max;
    }
};

85Maximal Rectangle

問題描述:

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 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 6.

問題解析:

1. 此題是求矩陣中爲1的最大的矩形元素個數。
2. 此題可以化爲上題中求柱狀圖最大面積。從矩陣第0行開始,每一行可以看成是一個heights數組,本行j位置爲1,如果上一行此列爲1,則heights[j] = height[j]+1,然後求最大矩形面積,利用上題代碼。


代碼如下:

class Solution {
public:
    // 利用堆棧求最大的數字
    void largestRectangleArea(vector<int>& heights, int &max) 
    {
        if(heights.empty())
            return;
        stack<int>sta;
        int cur = 0;
        int len = 0;
        int size = heights.size();
        for(int i=0; i<size; ++i)
        { 
            if(sta.empty() || sta.top() <= heights[i])
                sta.push(heights[i]);
            else
            {
                len = 0;
                while(!sta.empty() && sta.top() > heights[i])
                {
                    ++ len;
                    cur  = sta.top()*len;
                    if(cur > max)   max = cur;
                    sta.pop();
                }
                while(len)
                {
                    --len;
                    sta.push(heights[i]);
                }
                sta.push(heights[i]);
            }
        }
        while(!sta.empty())
        {
            ++ len;
            cur  = sta.top()*len;
            if(cur > max)   max = cur;
            sta.pop();
        }
        
        return;
    }
    int maximalRectangle(vector<vector<char>>& matrix) 
    {
        int m=matrix.size(), n=0;
        if(m>0) n=matrix[0].size();
        int max = 0;
        vector<int> heights(n, 0);
        for(int i=0; i<m; ++i)
        {
            for(int j=0; j<n; ++j)
            {
                heights[j] = matrix[i][j]=='1'? heights[j]+1 : 0;
            }
            largestRectangleArea(heights, max);
        }
        return max;
        
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章