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 ...
随机推荐
- SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题
SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. P ...
- mysql的itcast笔记
1 课程回顾 自定义标签&编码实战 1)自定义标签步骤: 1.1 编写标签处理器类,继承SimpleTagSupport类,覆盖doTag方法 1.2 在WEB-INF目录下建立tld文件,在 ...
- Java高并发(1)
1.同步和异步的区别和联系: 所谓同步,可以理解为在执行完一个函数或方法之后,一直等待系统返回值或消息,这时程序是出于阻塞的,只有接收到 返回的值或消息后才往下执行其它的命令. 异步,执行完函数或方法 ...
- [poj2342]Anniversary party树形dp入门
题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...
- bootstrap的popover()的使用
有一些选项是通过 Bootstrap 数据 API(Bootstrap Data API)添加或通过 JavaScript 调用的.下表列出了这些选项: 选项名称 类型/默认值 Data 属性名称 描 ...
- 移动构造和移动赋值与std::move
------------------------------------移动构造------------------------------------------ 传统的深拷贝深赋值 对于类中,含有 ...
- SCUT - 299 - Kaildls的数组划分 - dp - 高精
https://scut.online/p/299 \(dp[i][k]\) 为前 \(i\) 个数分 \(k\) 组的最大值,那么 $dp[i][k]=max_{p=1}^{i-1}{dp[p][k ...
- Search index
问题:查找字符串b在a中的起始位置,如果b不为a的子串,则返回-1 示例: 输入:a = "well", b = "el" 输出:1 输入:a=" ...
- 汇总:unity中弹道计算和击中移动目标计算方法
http://download.jikexueyuan.com/detail/id/432.html 弹道计算是游戏里常见的问题,其中关于击中移动目标的自动计算提前量的话题,看似简单,其实还是挺复杂的 ...
- 洛谷 P5162 WD与积木【多项式求逆】
设f[i]为i个积木能堆出来的种类,g[i]为i个积木能堆出来的种类和 \[ f[n]=\sum_{i=1}^{n}C_{n}^{i}g[n-i] \] \[ g[n]=\sum_{i=1}^{n}C ...