leetcode84 Largest Rectangle in Histogram
思路:
使用单调栈计算每个位置左边第一个比它矮的位置和右边第一个比它矮的位置即可。
实现:
#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的更多相关文章
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- 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 ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- python--面向对象(最全讲解)
http://www.cnblogs.com/Eva-J/articles/7293890.html 阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 ...
- docker 学习(一)什么是Docker
项目中用到docker,就学习一下.第一篇是介绍. Sandboxie(沙箱):一个虚拟系统程序,允许你在沙盘环境中运行浏览器或其他程序,因此运行所产生的变化可以随后删除.它创造了一个类似沙盒的独立作 ...
- 11_listview入门
listview是在安卓开发当中很常用的API. 以垂直滚动的列表的方式展示条目的控件. ListAdapter是一个桥梁,给ListView提供数据的.数据是由适配器来进行提供的.Adapter是数 ...
- Oracle数据去重
一.完全重复数据去重方法 具体思路是,首先创建一个临时表,然后将DISTINCT之后的表数据插入到这个临时表中;然后清空原表数据;再讲临时表中的数据插入到原表中;最后删除临时表. 对于表中完全重 ...
- netstat -st输出解析(二)
转自:http://perthcharles.github.io/2015/11/10/wiki-netstat-proc/ netstat -st输出的两个重要信息来源分别是/proc/net/sn ...
- 在Elasticsearch6.X中如何实现去重
1.前言 Elasticsearch有没有类似mysql的distinct的去重功能呢? 1)如何去重计数? 类似mysql: select distinct(count(1)) from my_ta ...
- js的prototype的详解(1)
一.什么是JavaScript中对象的prototype属性 JavaScript中对象的prototype属性,是用来返回对象类型原型的引用的.我们使用prototype属性提供对象的类的一组基本功 ...
- NOIP2015提高组 跳石头 ACM-ICPC2017香港 E(选择/移除+二分答案)
跳石头 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 NN ...
- HDU - 3001 Travelling(三进制状压dp)
Travelling After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best ch ...
- SEO优化之——rel=canonical(整合重复页)
用法一 如图所示 有三个页面,三个页面的访问路径都不一样,但是现实的内容page2和page3显示的内容和page1一样,这时候在三个page中分别都加入了<link res="can ...