题目描述:

给定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. 多线程 -- NSOperation

    NSOperation 此类不能直接使用 NSInvocationOperation NSBlockOperation 定义一个类继承与它 NSInvocationOperation 可以使用star ...

  2. Redis基础教程

    说明:本文中涉及的代码是c#所写,连接redis的第三方驱动为ServiceStack.Redis.连接redis的客户端软件为redis-desktop-manager. 一.Redis是什么 Re ...

  3. Material

    renderer.material  物理材质 实现二维图上的人物动作 新建Material,选择Shader(著色器)为transparent/diffuse(背景透明),将上图拉到背景图选项中. ...

  4. scala伴生对象,apply()及单例

    1:伴生对象与apply方法 如果一个class与一个object具有相同的名字,那么我们就认为它们互为伴生.object为class的伴生对象.如下图所示,object Apply为class Ap ...

  5. android 实现2张图片层叠效果

    如图: 代码: <RelativeLayout android:layout_width="match_parent" android:layout_height=" ...

  6. Netsharp介绍

    1.1     Netsharp是什么 Netsharp定义: Netsharp业务基础平台 = 1.系统框架 + 2.元数据 + 3.平台工具 + 4.基础业务 + 5.二次开发 此五个概念请参考什 ...

  7. 关于通过JS识别浏览器类型的方法

    JS检测浏览器类型的方法   网络上一般采用navigator.userAgent判断浏览器标识的办法,但是有个麻烦的问题是IE11不断升级之后,IE11的userAgent变成: "Moz ...

  8. Noip模拟考第三题——饥饿游戏

    饥饿游戏 (hungry.pas/c/cpp) [问题描述] Chanxer饿了,但是囊中羞涩,于是他去参加号称免费吃到饱的“饥饿游戏”. 这个游戏的规则是这样的,举办者会摆出一排 个食物,希望你能够 ...

  9. 【BZOJ】【1717】【USACO 2006 Dec】Milk Patterns产奶的模式

    后缀数组 o(︶︿︶)o 唉傻逼了一下,忘了把后缀数组的字典范围改回20001,直接21交了上去,白白RE了两发……sigh 既然要找出现了K次的子串嘛,那当然要用后缀数组了>_>(因为我 ...

  10. js的全局函数

    JS的全局函数,全局函数和window对象的函数不一样. 全局函数不属于任何一个内置对象. JS包含以下7个全局函数,用于一些常用的功能: escape(),unescape(); //编码,解码. ...