84. Largest Rectangle in Histogram-hard
84. 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 heights = [2,1,5,6,2,3],
return 10.
解析
直接的暴力的思路就是对于每一组子数组,找到其中最低的高度,然后求面积,进而求出最大的矩形面积。总共有n2个子数组,找最低高度是O(n)的操作,所以复杂度是O(n3)。
进一步,可以从每一个bar往两边走,以自己的高度为标准,直到两边低于自己的高度为止,然后用自己的高度乘以两边走的宽度得到矩阵面积。因为对于任意一个bar都计算了以自己为目标高度的最大矩阵,所以最好的结果一定会被取到。每次往两边走的复杂度是O(n),总共有n个bar,所以时间复杂度是O(n^2)。
最后说说最优的解法,思路跟Longest Valid Parentheses类似,主要维护一个栈,这个栈从低向上的高度是依次递增的。如果遇到当前bar高度比栈顶元素低,那么就出栈直到栈顶元素低于当前bar,出栈过程中检测前面满足条件的矩阵:
如果栈已经为空,说明到目前为止所有元素(当前下标元素除外)都比出栈元素高度要大(否则栈中肯定还有元素),所以矩阵面积就是高度乘以当前下标i。
如果栈不为空,那么就是从当前栈顶元素的下一个到当前下标的元素之间都比出栈元素高度大(因为栈顶元素第一个比当前出栈元素小的),所以矩阵面积就是高度乘以当前下标i减栈顶元素再减1。
注意最后还要对剩下的栈做清空并且判断,因为每次是对于前面的元素面积进行判断,所以循环结束中如果栈中仍有元素,还是要继续判断面积直到栈为空。
时间复杂度:O(n)
空间复杂度:O(1)
// 84. Largest Rectangle in Histogram
class Solution_84 {
public:
	int largestRectangleArea(vector<int>& heights) {
		int res = 0;
		stack<int> st; //存储递增的下标
		for (int i = 0; i < heights.size();i++)
		{
			while (!st.empty() && heights[st.top()]>heights[i]) //出栈操作,之前都是递增的
			{
				int h = heights[st.top()];
				st.pop();
				if (st.empty())
				{
					res = max(res, h*i);
				}
				else
				{
					res = max(res, h*(i - st.top()-1)); //当前区间[st.top+1,i-1]
				}
			}
			st.push(i);
		}
		while (!st.empty()) //递增的
		{
			int h = heights[st.top()];
			st.pop();
			int s = h * (st.empty() ? heights.size() : (heights.size() - st.top() - 1));
			res = max(res, s);
		}
		return res;
	}
};
题目来源
- 84. Largest Rectangle in Histogram-hard
 - leetcode之Largest Rectangle in Histogram
 - LeetCode --- 84. Largest Rectangle in Histogram
 
84. Largest Rectangle in Histogram-hard的更多相关文章
- 84. Largest Rectangle in Histogram
		
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
 - LeetCode 84. Largest Rectangle in Histogram 单调栈应用
		
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
 - 刷题84. Largest Rectangle in Histogram
		
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
 - 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
 - 【LeetCode】84. Largest Rectangle in Histogram
		
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
 - 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积  85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
		
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
 - 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
		
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
 - [LeetCode#84]Largest Rectangle in Histogram
		
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
 - LeetCode OJ 84. 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 ...
 
随机推荐
- 使用Unity解耦你的系统—PART4——Unity&PIAB
			
在前面几篇有关Unity学习的文章中,我对Unity的一些常用功能进行介绍,包括:Unity的基本知识.管理对象之间的关系.生命周期.依赖注入等,今天则是要介绍Unity的另外一个重要功能——拦截(I ...
 - 深入浅出Spring(二) IoC详解
			
上次的博客深入浅出Spring(一)Spring概述中,我给大家简单介绍了一下Spring相关概念.重点是这么一句:Spring是为了解决企业应用开发的复杂性而创建的一个轻量级的控制反转(IoC)和面 ...
 - 【数学】Codeforces Round #470 (Div2) B
			
题目链接:http://codeforces.com/contest/948/problem/B 题目大意:有一个大于等于3的数X0,任意选择一个小于X0的质数P0,乘以一个数k使k*P0>=X ...
 - java项目日志系统的总结
			
目录 日志系统归类以及关系 日志的三个组件 slf4j的使用 项目中构建日志系统 使用例子 日志系统归类以及关系 常用的日志框架: slf4j.logback .log4j.log4j2.JUL(ja ...
 - mysql-8.0.12安装和配置
			
1.下载Mysql8.0.12压缩包.下载地址:https://dev.mysql.com/downloads/file/?id=480557 2.解压文件到本地指定目录.这里我的mysql根目录是: ...
 - django常见问题小结,细节容易忽视
			
中文URL:这个其实是很常识的东西,但是之前做web一直没注意过,在使用HttpResponseRedirect的时候,如果Redirect的URL中带中文的话,会报UnicodeEncodeErro ...
 - 【BZOJ 4527】 4527: K-D-Sequence (线段树)
			
4527: K-D-Sequence Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 145 Solved: 59 Description 我们称一个 ...
 - 【刷水-二分答案】BZOJ1650 & BZOJ1639
			
BZOJ1650-[Usaco2006 Dec]River Hopscotch 跳石子 [题目大意] 数轴上有n个石子,第i个石头的坐标为Di,现在要从0跳到L,每次条都从一个石子跳到相邻的下一个石子 ...
 - 【LCA/tarjan】POJ1470-Closest Common Ancestors
			
[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...
 - bzoj 1269 bzoj 1507   Splay处理文本信息
			
bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...