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. oracle 权限管理

    系统权限 系统权限需要授予者有进行系统级活动的能力,如连接数据库,更改用户会话.建立表或建立用户等等.你可以在数据字典视图SYSTEM_PRIVILEGE_MAP上获得完整的系统权限.对象权限和系统权 ...

  2. Mybatis的学习总结二:使用Mybatis对表进行CRUD操作【参考】

    一.使用Mybatis对表进行CRUD操作------基于XML的实现 1.定义SQL的映射文件 2.在conf.xml中进行注册. 2.创建测试类 [具体过程参考:Mybatis的学习总结一] 二. ...

  3. ios 设置label的高度随着内容的变化而变化

    好吧 步骤1:创建label _GeRenJianJie = [[UILabel alloc]init]; 步骤2:设置label _GeRenJianJie.textColor = RGBAColo ...

  4. 『重构--改善既有代码的设计』读书笔记----Change Value to Reference

    有时候你会认为某个对象应该是去全局唯一的,这就是引用(Reference)的概念.它代表当你在某个地点对他进行修改之后,那么所有共享他的对象都应该在再次访问他的时候得到相应的修改.而不会像值对象(Va ...

  5. Java学习----日期函数

    Date   Calendar public class TestDate { private Date date; Calendar calendar; public TestDate() { // ...

  6. CentOS6.5 yum安装桌面环境

    安装原因 安装centos6.5时选择了minimal CentOS最小化安装方式 需要使用浏览器拨号连接内网 安装过程 通过yum grouplist查询在 group 软件包中,Desktop.D ...

  7. C#一个字符串的加密与解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  8. 使用php发送电子邮件(phpmailer)

    在项目开发过程中,经常会用到通过程序发送电子邮件,例如:注册用户通过邮件激活,通过邮件找回密码,发送报表等.这里介绍几种通过PHP发送电子邮件的 方式(1)通过mail()函数发送邮件(2)使用fso ...

  9. Winbind authentication against active directory

    Winbind authentication against active directory Description This tip will describe how to configure ...

  10. strstr_while模型

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...