LeetCode 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.
示例
Given heights =
[2,1,5,6,2,3]
,
return10
.
解题思路
第一阶段:
拿到这种题,首先想怎么用最暴力的方法解决这个问题,于是自然而然的想出来枚举的方法,即利用二重循环枚举所有可能的区间,接着再这区间中找出最小的值,然后用(最小值 * 区间宽度)求出每个区间的最大值,最后从这些最大值中找出最大解。
这种解法的时间复杂度是O(n^3),一般都会超时的,那么我们就会想着降低时间复杂度,即去冗余的方法(冗余分为重复计算和不需要计算两种)
第二阶段:
那么哪里存在冗余呢?第一个想到的应该是区间内求最小值时存在重复计算,我们可以使用空间换时间的方法将之前算过的最小值存储下来,这样求最小值[i][j] 就等于MIN( 最小值[i][j-1] , j) ,这样我们就时间复杂度降低到了O(n^2)
第三阶段:
通过推演又进一步发现了冗余的地方,比方说对于[9, 8, 10, 3, 12, 11,15]来说,虽然我们枚举了所有可能的区间,计算了其区间内的最小值,但你有没有发现,其中大多数的最小值都是 3, 那么我们能不能改变思路减少该冗余呢?
于是乎我们可以采用枚举最小值的算法,即假设每一个点都是最小值,然后我们计算出其作为最小值可以向左右两边延伸的最大区间(如果该点左边的点值大于该点,则继续往左比较,知道某点比该点值小为止)
最后可以算出每一个点作为最小值所包括的最大面积。面积 = n点高度 * (n右边界 - n左边界 + 1 )
第四阶段:
上一阶段的时间复杂度似乎并没有减少,那么肯定这种新思路引来了新的冗余,这种冗余在哪里呢?
我们发现在找一个点的左右最大区间时存在重复计算,因为如果n点比n - 1点的值大的话,那么n点左边界应该大于等于n - 1点的左边界,于是乎我们可以存储下每个点的左右边界,避免很多重复计算
最终思路:
- 枚举所有点,将其作为最小值
- 记录每个点的左右边界(计算n点左边界的方法是:判断n是否比n - 1小,如果成立则跳到n - 1 点的左边界x, 比较n是否比x小,如此循环,知道求出左边界)
- 枚举每一个点的最大面积,计算最大解
完整代码
public class Solution {
public int largestRectangleArea(int[] heights) {
int n = heights.length;
if (n == 0) {
return 0;
}
// 求左边的边界
int[] left = new int[n];
left[0] = 0;
for (int i=1;i<n;++i) {
int A = i;
while (A > 0 && heights[A - 1] >= heights[i]) {
A = left[A - 1];
}
left[i] = A;
}
// 求右边的边界
int[] right = new int[n];
right[n - 1] = n - 1;
for (int i=n-2;i>=0;--i) {
int A = i;
while (A < n - 1 && heights[A + 1] >= heights[i]) {
A = right[A + 1];
}
right[i] = A;
}
// 枚举每一个最小值的最大面积
int ans = 0;
for (int i=0;i<n;++i) {
ans = Math.max(ans, heights[i] * (right[i] - left[i] + 1));
}
return ans;
}
}
LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形的更多相关文章
- [LeetCode] 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 is ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】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 is ...
- [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析
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】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
随机推荐
- Repcached实现memcached复制
1.介绍 repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的master/slave都是可读写的,而且可以相互同步,如果 ma ...
- fopen参数
此文用于学习交流 原作:http://www.cnblogs.com/ai616818/archive/2012/04/26/2470918.html FILE * fopen(const char ...
- Freemaker配置文件详解
classic_compatible=true ##如果变量为null,转化为空字符串,比如做比较的时候按照空字符做比较 whitespace_stripping=true ...
- RobotFramework中解析中文报错UnicodeDecodeError
在RobotFramework中解析一段包含中文的字符串时遇到下面的报错: FAIL : UnicodeDecodeError: 'ascii' codec can't decode byte 0xe ...
- mvp架构解析
MVP现在已经是目前最火的架构,很多的框架都是以MVP为基础,甚至于Google自己都出一个MVP的开源架构.https://github.com/googlesamples/android-arch ...
- 从Properties得到数据到gson转换为json
从上一篇得到properties里的数据 Map<String,String> map = new HashMap<String,String>(); Enumeration& ...
- python终端中退格键,方向键不能使用
在安装Python的时候先安装 yum install readline-devel.x86_64 -y 如果python已经安装过了就重新安装 ./configure make all mak ...
- 认识J2SE
1. J2SE的定义 J2SE:全称为Java 2 Standard Edition.Java 2平台包括:标准版(J2SE).企业版(J2EE)和微缩版(J2ME)三个版本. J2SE主要包括UI. ...
- java封装的方法
java封装是由Java是面向对象程序设计语言的性质决定的,面向对象程序设计语言的三大特性之一就是封装.封装其实就是包装的意思,从专业的角度来看,就是把对象的所有组成部分组合在一起,保护私有属性. 如 ...
- 疯狂的 JAVA 后++
一.x++ 所以执行完x++之后,局部变量区的x值,直接为2: iinc: 指定int型变量增加指定的值,注意是变量,我的解释是iinc直接对局部变量操作,而不是对操作栈进行操作! ★★★★ OUTP ...