Largest 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 height = [2,1,5,6,2,3],
return 10.

参考:http://blog.csdn.net/abcbc/article/details/8943485

C++实现代码:

#include<iostream>
#include<vector>
#include<stack>
using namespace std;

class Solution
{
public:
    int largestRectangleArea(vector<int> &height)
    {
        if(height.empty())
            return 0;
        int maxArea=0;
        int i=0;
        int n=height.size();
        stack<int> st;
        int start;
        for(i=0;i<n;i++)
        {
            if(st.empty()||height[i]>height[st.top()])
                st.push(i);
            else
            {
                start=st.top();
                st.pop();          //注意求宽度时,是减去当前元素的前一个栈顶元素的index
                int width=st.empty()?i:i-st.top()-1;
                maxArea=max(width*height[start],maxArea);
                i--;//处理到栈为空或者栈中的元素都比当前处理的元素小为止
            }
        }
        while(!st.empty())
        {
            start=st.top();
            st.pop();
            int width=st.empty()?n:n-st.top()-1;
            maxArea=max(width*height[start],maxArea);
        }
        return maxArea;
    }
};

int main()
{
    Solution s;
    vector<int> height={1,2,2};
    cout<<s.largestRectangleArea(height)<<endl;
}

自己写的一个O(n^2)超时了。

#include<iostream>
#include<vector>
#include<climits>
using namespace std;

class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        if(height.empty())
            return 0;
        int i,j;
        int minH;
        int maxArea=0;
        int n=height.size();
        for(i=0;i<n;i++)
        {
            minH=height[i];
            for(j=i;j<n;j++)
            {
                minH=min(minH,height[j]);
                maxArea=max(maxArea,minH*(j-i+1));
            }
        }
        return maxArea;
    }
};

int main()
{
    Solution s;
    vector<int> height={2,1,5,6,2,3};
    cout<<s.largestRectangleArea(height)<<endl;
}

 

时间: 2024-09-20 05:43:32

Largest Rectangle in Histogram的相关文章

[LeetCode]*84.Largest 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 large

POJ 2559 / HDU 1506 Largest Rectangle in a Histogram (DP)

Largest Rectangle in a Histogram http://poj.org/problem?id=2559 Time Limit: 1000MS Memory Limit: 65536K Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may

[LeetCode]*85.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. 思路 对于上图的一个01矩阵.我们可以一行一行的分析,假设第三行,我们按列扫描,遇到0时,柱子断开,重新形成柱子,遇到1时柱子高度加一.这样的话,我们就可以把问题转换为[LeetCode]*84.Largest Rectangle in Histogram求

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

直方图中最大矩形面积

Largest Rectangle in Histogram 直方图中最大矩形面积 一个直方图是由许多矩形组成,在给定的直方图中找出最大的矩形面积.为了简化问题,假定所有矩形宽度都为1个单位. 例如,下面的直方图中有7个矩形,高度分别是(6,2,5,4,5,2,6).最大的矩形面积是12(如下图所示,最大矩形面积用红色方框标出) 下面给出的解决方法时间复杂度为O(n).矩形面积的计算公式为底*高.对于直方图中的每个矩形'x'(例如图中高度为6,2或5的矩形)以该矩形的高度为高(因为在直方图中最大

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. 参考:http://xpentium.blog.163.com/blog/static/22737815020147795123240/ 题目是说matrix中的元素可能有0和1,找出一个子矩阵,这个子矩阵要求是全为1中最大的.如果暴力求解的话,复杂度将直逼O(n

SEERC 2004 / UVa 1330 City Game (扫描)

1330 - City Game Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 Bob is a strategy game programming specialist. In his new city building game the ga