思路:

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

实现:

 #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. 洛谷P2024食物链——并查集补集的灵活运用

    题目:https://www.luogu.org/problemnew/show/P2024 自己在做本题时最大的障碍就是:不会在一个集合的father改变时把相应的补集也跟着改变. 借鉴题解后,才明 ...

  2. WPF架构分析

    1.DisptcherObject提供了线程和并发模型,实现了消息系统. 2.DependencyObject提供了更改通知,实现了绑定,样式. 3.Visual是托管API和非托管API(milco ...

  3. rt-thread的定时器管理源码分析

    1 前言 rt-thread可以采用软件定时器或硬件定时器来实现定时器管理的,所谓软件定时器是指由操作系统提供的一类系统接口,它构建在硬件定时器基础之上,使系统能够提供不受数目限制的定时器服务.而硬件 ...

  4. python+ mysql存储二进制流的方式

    很多时候我们为了管理方便会把依稀很小的图片存入数据库,有人可能会想这样会不会对数据库造成很大的压力,其实大家可以不用担心,因为我说过了,是存储一些很小的图片,几K的,没有问题的! 再者,在这里我们是想 ...

  5. Python3解leetcode Same TreeBinary Tree Level Order Traversal II

    问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  6. sql基础内容2

    -- day16课程内容 --CREATE DATABASE day16;USE day16; -- *************一.数据约束********************------ 1.1 ...

  7. 利用NDK崩溃日志查找BUG

    转自:http://www.tuicool.com/articles/qQNfUfe 背景介绍 本文主要内容: 利用android的crash log来对c++开发的android应用进行错误定位. ...

  8. 《Java多线程编程核心技术》读后感(十四)

    单例模式与多线程 立即加载/饿汉模式 立即加载就是使用类的时候已经将对象创建完毕,常见的实现办法就是直接new实例化. 立即加载/饿汉模式实在调用方法前,实例已经被创建了 package Six; p ...

  9. hdu3047 Zjnu Stadium (并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  10. java之Date(日期)、Date格式化、Calendar(日历)

    参考http://how2j.cn/k/date/date-date/346.html Date(日期) Date类 注意:是java.util.Date; 而非 java.sql.Date,此类是给 ...