思路:

使用单调栈计算每个位置左边第一个比它矮的位置和右边第一个比它矮的位置即可。

实现:

 #include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int largestRectangleArea(vector<int>& heights)
{
int n = heights.size();
vector<int> l(n, -);
stack<int> s;
s.push();
for (int i = ; i < n; i++)
{
if (heights[s.top()] < heights[i])
{
l[i] = s.top();
s.push(i);
}
else
{
while (!s.empty() && heights[s.top()] >= heights[i])
s.pop();
if (!s.empty()) l[i] = s.top();
s.push(i);
}
}
while (!s.empty()) s.pop();
s.push(n - );
vector<int> r(n, n);
for (int i = n - ; i >= ; i--)
{
if (heights[s.top()] < heights[i])
{
r[i] = s.top();
s.push(i);
}
else
{
while (!s.empty() && heights[s.top()] >= heights[i])
s.pop();
if (!s.empty()) r[i] = s.top();
s.push(i);
}
}
int ans = ;
for (int i = ; i < n; i++)
{
ans = max(ans, heights[i] * (r[i] - l[i] - ));
}
return ans;
}
};
int main()
{
int a[] = {, , , , , };
vector<int> v(a, a + );
cout << Solution().largestRectangleArea(v) << endl;
return ;
}

leetcode84 Largest Rectangle in Histogram的更多相关文章

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

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

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

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

  3. 【LeetCode】84. Largest Rectangle in Histogram

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

  4. leetcode Largest Rectangle in Histogram 单调栈

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

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

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

  6. leetcode之Largest Rectangle in Histogram

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

  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 ...

随机推荐

  1. 单机 Oracle 11g(11.2.0.4)手动打补丁PSU(11.2.0.4.8)

    环境说明:database : 11.2.0.4 x64os: centos6.7 x64 准备内容:OPatch : p6880880_112000_Linux-x86-64.zipDB PSU : ...

  2. poj3254二进制放牛——状态压缩DP

    题目:http://poj.org/problem?id=3254 利用二进制压缩状态,每一个整数代表一行的01情况: 注意预处理出二进制表示下没有两个1相邻的数的方法,我的方法(不知为何)错了,看到 ...

  3. Python操作MySQL数据库9个实用实例

    用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示.下边是打包了32与64版本. MySQL-python-1.2.3.win32-py2.7.exe MySQL-pytho ...

  4. DTP模型之一:(XA协议之三)MySQL数据库分布式事务XA优缺点与改进方案

    1 MySQL 外部XA分析 1.1 作用分析 MySQL数据库外部XA可以用在分布式数据库代理层,实现对MySQL数据库的分布式事务支持,例如开源的代理工具:ameoba[4],网易的DDB,淘宝的 ...

  5. JavaScript高级程序设计学习笔记第十四章--表单

    1.在 HTML 中,表单是由<form>元素来表示的,而在 JavaScript 中,表单对应的则是 HTMLFormElement 类型. HTMLFormElement 继承了 HT ...

  6. Linux命令总结_命令执行顺序

    有时候,我们需要一个命令执行完之后再去执行另一个命令,使用 &&和 ||可以完成 这样的功能,相应的命令可以是系统命令或shell脚本 Shell还提供了在当前shell或子shell ...

  7. 无法搜索联机扩展 因为尝试与服务器联系 Visual studio 怎么解决?

    根目录: devenv.exe.config 编辑: 修改如下即可: <system.net> <defaultProxy useDefaultCredentials="t ...

  8. 点击实现CSS样式切换

    如图所示 代码如下图: 特别要注意的是:a标签不会继承上级的color,所以要单独为其设置 参看代码(并非上图代码)如下: <!DOCTYPE html> <html> < ...

  9. 715. Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  10. 阿里云(centos)下svn 服务器搭建

    安装说明 系统环境:阿里云centos安装方式:yum install subversion 检查已安装版本 #检查是否安装了低版本的SVN[root@localhost /]# rpm -qa su ...