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.

[ 解题思路 ]

问题: 求直方图中面积最大的矩形。

直方图中面积最大的矩形,必然以某一个柱作为高,左侧、右侧最近且矮于该柱的柱为宽边界。

考虑上面长度为 7 的直方图(图片来源), {6, 2, 5, 4, 5, 2, 6}。面积最大矩形的是红框中的矩形,面积为 12 。

方案:

“i最大矩形”,表示为 i 柱为高,左侧、右侧最近且矮于该 i 柱的柱为宽边界的矩形。

"iLeft" , 表示左侧最近且矮于 i 柱的柱

"iRight", 表示右侧最近且矮于 i 柱的柱

第一步,分别求出 n 个 “i最大矩形”,( i : 0->(n-1) )。第二步,找过第一步中最大值,即为原问题的解。

若每次单独求 i 柱的 "iLeft", "iRight",则算法复杂度为 O(n*n)。可以利用栈 s ,巧妙地将时间复杂度降为 O(n)。

  • 当栈为空 或 s.peak < h[i] 时,则将 i 压入栈顶。i++。
  • 当 h[i] <= s.peak 时,对于 s.peak 柱来说, h[i] 为 "iRight",  栈中 s.peak 的前一个柱为 "iLeft",则弹出 s.peak,并计算 s.peak 的 "i最大矩形"
     int largestRectangleArea(vector<int>& height) {

         int maxArea = ;

         vector<int> stack;

         int i = ;
while ( i < height.size() ) {
if (stack.size() == || height[stack.back()] < height[i]) {
stack.push_back(i);
i++;
}else{
int tmpH = height[stack.back()];
stack.pop_back(); int tmpW = stack.empty() ? i : (i - stack.back() - ); int area = tmpH * tmpW;
maxArea = max(area, maxArea);
}
} while ( !stack.empty() ) {
int tmpH = height[stack.back()];
stack.pop_back(); int tmpW = stack.empty() ? (int)height.size() : (int)height.size() - stack.back() - ; int area = tmpH * tmpW;
maxArea = max(area, maxArea);
} return maxArea;
}

参考资料:

Largest Rectangular Area in a Histogram | Set 2, GeeksforGeeks

[LeetCode] Largest Rectangle in Histogram 解题思路的更多相关文章

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

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

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

  3. leetcode Largest Rectangle in Histogram 单调栈

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

  4. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  5. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

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

  6. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  7. [LeetCode] Largest Rectangle in Histogram

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

  8. leetcode -- Largest Rectangle in Histogram TODO O(N)

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

  9. LeetCode——Largest Rectangle in Histogram

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

随机推荐

  1. jQuery 遍历祖先

    祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...

  2. C++重载流插入运算符和流提取运算符【转】

    C++的流插入运算符“<<”和流提取运算符“>>”是C++在类库中提供的,所有C++编译系统都在类库中提供输入流类istream和输出流类ostream.cin和cout分别是 ...

  3. Java学习----接口

    1. interface关键字 2. 接口中的方法全部是抽象方法,不能被实例 3. 接口中的成员变量: public static final 4. 当子类实现接口的时候,必须覆盖接口中所有的方法 / ...

  4. 我也来玩玩WinForm~BeginInvoke让用户体验更好!

    前言 先说明一下,本人不太做winform的项目,工作10年以来,一直奋斗在webform的舞台上,今天有机会也接触了一下winform,下面对工作中用到的BeginInvoke方法作一下说明,和大家 ...

  5. 【随记】修复TortoiseGit文件夹和文件状态图标不显示问题

    一. 运行环境: 操作系统 Windows 10 64bit TortoiseGit (2.2.0.0) 64bit msysgit(2.9.2.1) 64bit 注意:请确保环境正确,软件的位数相匹 ...

  6. 子元素增加margin-top会增加给父元素的问题

    假设我们有如下代码 <div id="father" style="height:400px;width:400px;background:#e4393c;&quo ...

  7. linux用户管理,linux用户口令管理,linux用户组管理,linux用户权限管理详解

    linux用户管理 http://www.qq210.com/shoutu/android 用户账号的添加(新加用户需添加用户口令) :增加用户账号就是在/etc/passwd文件中为新用户增加一条记 ...

  8. MAYA 多线程

    ''' Usage: def timerTest(): print 'Hello World!' #create and start a timer timer = Timer(30, timerTe ...

  9. "Invalid bound statement (not found): com.sitech.admin.dao.TbOpenAbilityInfoDao.findAbilityReadyUp"mybatis配置文件bug

    问题描述: 通常在正常启动某项工程后操作某个功能时抛出的bug: org.apache.ibatis.binding.BindingException: Invalid bound statement ...

  10. [BZOJ 3144] [Hnoi2013] 切糕 【最小割】

    题目链接:BZOJ - 3144 题目分析 题意:在 P * Q 的方格上填数字,可以填 [1, R] . 在 (x, y) 上填 z 会有 V[x][y][z] 的代价.限制:相邻两个格子填的数字的 ...