【Leetcode】Largest Rectangle in Histogram
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.
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int max_area = ;
stack<int> s;
height.push_back();
int i = ;
while (i < height.size()) {
if (s.empty() || height[s.top()] < height[i]) {
s.push(i++);
} else {
int t = s.top();
s.pop();
max_area = max(max_area,
height[t] * (s.empty() ? i : i - s.top() - ));
}
}
return max_area;
}
};
O(n2)的算法还是很好想的,但是如果借用数据结构--栈,可以使复杂度降低。
从左向右扫描数组,如果bar递增,则入栈,遇到第一个下降的bar时,开始出栈,计算从该bar(不含)到栈顶元素(含)之间形成的矩形面积,直到遇到栈里第一个低于它的bar,此时可以继续向前扫描下一个元素。
栈里维护的是递增序列。
【Leetcode】Largest Rectangle in Histogram的更多相关文章
- 【题解】Largest Rectangle in a Histogram [SP1805] [POJ2559]
[题解]Largest Rectangle in a Histogram [SP1805] [POJ2559] [题目描述] 传送: \(Largest\) \(Rectangle\) \(in\) ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- 【leetcode刷题笔记】Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- python之模块(在命令行当中使用pip install 模块来进行模块的安装)
模块:在程序设计中,为完成某一功能所需的一段程序或子程序:或指能由编译程序.装配程序等处理的独立程序单位. 在我们编程的时候我们如果将所有的文件都放在那个py文件中,我们的py文件会很大,这样也很不好 ...
- 给Activity切换过程添加动画效果
首先,在资源文件中定义一些动画效果 例如: <scale android:duration="@android:integer/config_mediumAnimTime" ...
- php apc 安装
APC简介 APC(Alternative PHP Cache)是一个PHP缓存.它在内存中存储PHP页面并且减少了硬盘的I/O.这对于性能的提升十分明显.你甚至可以在CPU使用率下降50%的情况下提 ...
- PCL — Point Pair Feature 中层次点云处理
博客转载自:http://www.cnblogs.com/ironstark/p/5971976.html 机器人视觉中有一项重要人物就是从场景中提取物体的位置,姿态.图像处理算法借助Deep Lea ...
- PCL—点云分割(RanSaC)低层次点云处理
博客转载自:http://blog.csdn.net/app_12062011/article/details/78131318 点云分割 点云分割可谓点云处理的精髓,也是三维图像相对二维图像最大优势 ...
- windows平台下用C#访问HBase
Hadoop中的HBase有多种数据访问方式,ubuntu里可以用hbase shell查看操作hbase数据库,但windows平台下需要用thrift对它进行访问. 例如hadoop安装在/usr ...
- Umbraco中如何找到home node
在一个Umbraco项目中,我们经常会出现需要找到这个项目的home node的情况, 那么如何来找到项目的home node呢 方法如下: 1. 在View中 @inherits Umbraco.W ...
- java全栈day11----构造方法 综合案例
构造方法 在开发中经常需要在创建对象的同时明确对象的属性值,比如员工入职公司就要明确他的姓名.年龄等属性信息. 那么,创建对象就要明确属性值,那怎么解决呢?也就是在创建对象的时候就要做的事情,当使用n ...
- HTML 4.0 触发事件
HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 在现代浏览器中都内置有大量的事件处理器.这些处理器会监视特 ...
- Spring 第一个程序-Bean的定义和注册
定义一个实现接口的方法 创建xml文件,这个xml文件就是个所谓的容器 不使用spring容器和使用spring容器创建对象执行代码的区别如下: 下面说一下ApplicationC ...