题目描述:

给定n个非负整数height[n],分别代表直方图条的高,每个条的宽设为1,求直方图中面积最大的矩形的面积

题目来源:

http://oj.leetcode.com/problems/largest-rectangle-in-histogram/

题目分析:

维护一个栈,保存直方图条的下标,当当前栈为空或者栈顶的下标所表示的元素不大于当前元素时,入栈,否则出栈,直到可以把当前元素压入栈中

(1)对于当前栈,假设序列为a1, a2,...ai, ai+1, a...栈顶,那么处于ai和ai+1之间的元素一定大于ai+1,如果他们中的最小元素小于等于ai+1,那么它一定在栈中,故栈中处于ai和ai+1之间的元素一定大于ai+1

(2)计算矩形的面积,可以考虑以待计算的元素为中心,向右扩展最远,并且向左扩展最远

(3)出栈时,计算以刚出栈的元素为高的最大矩形,它向左扩展最远到栈中的下一个元素,向右扩展最远到当前元素(因为当前元素比他小)

时间复杂度:O(n)
示例代码:
int maxArea(vector<int> vi) {
stack<int> st;
int maxArea = , i = ; while(i <= n) {
if(st.empty() || vi[st.top()] <= vi[i]) {
st.push(i++);
} else {
int tmp = st.top();
st.pop();
maxArea = max(maxArea, vi[tmp] * (st.empty() ? i : i - st.top() - ));
}
} return maxArea;
}

Largest Rectangle in Histogram-最大长方形的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  3. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  4. 47. Largest Rectangle in Histogram && Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  6. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

  7. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  8. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  9. LeetCode: Largest Rectangle in Histogram 解题报告

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  10. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

随机推荐

  1. cocos2dx中如何从一张图片中切割一部分显示成小图片

    1.通常我们拿到的资源中,通常都是许多张小图片压缩到一张图片里了,我们如何在使用的时候把它切割出来呢? 2.例如我们要把上面这张图片按组分隔开来 CCSprite* newGameNormal = C ...

  2. hibernate3连oracle的各种坑。。

    坑一:驱动错误导致sql查询不了,升级驱动到最新版即可 2.通过构造函数封装数据时,如果报错无法实例化并且不是因为字段不对应导致的,可以试试把float改为Float之类的包装类

  3. The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

    如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...

  4. WinForm Control - DataGridView

    http://blog.csdn.net/fangxing80/article/details/1561011 .NET 2.0 - WinForm Control - DataGridView 编程 ...

  5. 1564: [NOI2009]二叉查找树 - BZOJ

    Description Input Output只有一个数字,即你所能得到的整棵树的访问代价与额外修改代价之和的最小值.Sample Input4 101 2 3 41 2 3 41 2 3 4Sam ...

  6. Java多jdk安装

    1.安装jdk 2.配置 1.安装(略) 2.配置 2.1 regedit 注册表修改,假定已经安装jdk1.6,现在更换为jdk1.7 注: 修改红色框中CurrentVersion为jdk1.7 ...

  7. Leetcode#148 Sort List

    原题地址 链表归并排序 真是恶心的一道题啊,哇了好多次才过. 代码: void mergeList(ListNode *a, ListNode *b, ListNode *&h, ListNo ...

  8. ios 环境配置网址

    http://blog.csdn.net/cwb1128/article/details/18019751

  9. SEO优化的黑帽手法是否值得使用?

    PR劫持 可能很多人也会听到说,什么网站权重越高越好,这也就是后面越来越多人都对谷歌的PR的宣传看的很重,自建站的都追求PR值,权重越高代表这个网站越受信任. 比如一个新站PR值为0,一个老站PR为6 ...

  10. Nginx SPDY Pagespeed模块编译——加速网站载入

    在看<Web性能权威指南>的时候,看到了SPDY这货,于是便开始折腾起了这个了,也顺便把pagespeed加了进去. Nginx SPDY 引自百科~~ SPDY(读作“SPeeDY”)是 ...