Hard-题目27:84. Largest Rectangle in Histogram

转载 2016年05月31日 23:39:08

题目原文:
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.
题目大意:
给出一个直方图,求图中最大矩形面积。
题目分析:
首先是朴素解法,遍历所有左端点为i,右端点为j的矩形的面积。复杂度O(n2),毫无疑问tle。
参考网上大神的解法,我理解如下:
维护一个递增的栈,遍历数组时,如果当前高度height[i]>stack.top则入栈,反之弹出栈直至栈顶元素<height[i],然后计算出最后弹出那个边“左右延伸”所得的矩形的面积,并更新最大值,然后height[i]入栈。
注意栈中存的是下标,这样便于计算宽度=下标差。
源码:(language:cpp)

class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        if(height.size() == 0) return 0; 
        stack<int> st;
        int MAX = 0;
        height.push_back(0);
        int leftarea = 0, rightarea = 0;
        for(int i = 0; i < height.size(); ++i){
            while(!st.empty() && height[st.top()] > height[i]){
                int tmp = st.top();
                st.pop();
                leftarea = (st.empty() ? tmp + 1 : tmp - st.top()) * height[tmp]; //以tmp为高度,tmp所在柱以及向左延伸出来的矩形面积
                rightarea = (i - tmp - 1) * height[tmp]; //以tmp为高度,向右边延伸出来的矩形面积
                if((leftarea + rightarea) > MAX) MAX = (leftarea + rightarea);
            }
            st.push(i);
        }
        return MAX;
    }
};

成绩:
24ms,18.83%,24ms,58.54%
cmershen的碎碎念:
以后的问题大多超越我的能力范围,解题步骤均摘抄自他人博客。

相关文章推荐

Leetcode题解14 84. Largest Rectangle in Histogram(hard)

Given n non-negative integers representing the histogram’s bar height where the width of each bar is...
  • Xd_Yu
  • Xd_Yu
  • 2016年05月06日 11:11
  • 419

LeetCode 84. Largest Rectangle in Histogram(Hard)

Largest Rectangle in Histogram(Hard)Description Given n non-negative integers representing the hist...

[leetcode] 84. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar i...

leetcode No84. Largest Rectangle in Histogram

Question: Given n non-negative integers representing the histogram's bar height where the width o...

84. Largest Rectangle in Histogram(dp)

题目: Given n non-negative integers representing the histogram's bar height where the width of ea...

LeetCode-84.Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is...

第四周:84. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is...

leetcode84: Largest Rectangle in Histogram

84. Largest Rectangle in Histogram height的内容是 [5,6,7,8,3],特点是除了最后一个,前面全部保持递增,且最后一个立柱的高度小于前面所有立柱高度。对...

[LeetCode] 84. Largest Rectangle in Histogram

[LeetCode] 84. Largest Rectangle in HistogramGiven n non-negative integers representing the histogra...

【LeetCode】84. Largest Rectangle in Histogram 解法及注释

84. Largest Rectangle in Histogram Total Accepted: 57808 Total Submissions: 239940 Difficulty: Har...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目27:84. Largest Rectangle in Histogram
举报原因:
原因补充:

(最多只允许输入30个字)