84.Largest Rectangle in histogram---stack
题目链接:https://leetcode.com/problems/largest-rectangle-in-histogram/description/
题目大意:在直方图中找出最大的矩形面积。例子如下:

法一:暴力,无任何优化,超时了。对于每个高度,分别向左和向右查找能到达的最远下标(在目前的高度情况下)。代码如下:
public int largestRectangleArea(int[] heights) {
int ma = 0;
for(int i = 0; i < heights.length; i++) {
//向左
int left = i;
for(; left >= 0 && heights[left] >= heights[i]; left--);
//向右
int right = i;
for(; right < heights.length && heights[right] >= heights[i]; right++);
int sum = (right - left - 1) * heights[i];
if(sum > ma) {
ma = sum;
}
}
return ma;
}
法二:换了一种思维方式的暴力,依旧超时。不再从中间向两边扩展,而是每到一个点,就找其局部峰值,然后由局部峰值点向前查找每个矩形面积,比较得最大值。代码如下:
public int largestRectangleArea(int[] heights) {
int ma = 0;
for(int i = 0; i < heights.length; i++) {
//找局部峰值
//如果不是目前峰值,则跳过
if(i + 1 < heights.length && heights[i] < heights[i + 1]) {
continue;
}
//如果是目前峰值,则向前计算矩形,会将目前峰值前面所有可能的矩形面积都计算一遍
//所以虽然这个方法没有在每个点上向左右两边扩展计算所有可能的矩形面积,但是其实也变相计算了所有可能的矩形面积,只是换了种思维方式
int mi = heights[i];
for(int j = i; j >= 0; j--) {
mi = Math.min(mi, heights[j]);
int sum = mi * (i - j + 1);
ma = Math.max(ma, sum);
}
}
return ma;
}
法三:用stack优化暴力,其实也计算了所有可能的矩形面积,只是优化了遍历方式,据说时间复杂度是o(n)?比较怀疑。代码如下(耗时23ms):
public int largestRectangleArea(int[] heights) {
//stack中存递增高度
Stack<Integer> s = new Stack<Integer>();
int ma = 0;
for(int i = 0; i < heights.length; i++) {
//如果栈顶高度比当前高度高,则退栈
//由目前栈顶高度向右计算可能的最大矩形面积,其实最终也把每个点所有可能的矩形面积都计算了一遍,但是由于优化计算,效率上好了很多
while(!s.isEmpty() && heights[s.peek()] >= heights[i]) {
int cur = s.pop();
//计算矩形面积
ma = Math.max(ma, heights[cur] * (s.isEmpty() ? i : (i - s.peek() - 1)));
}
//如果与栈顶是递增关系,则压栈
s.push(i);
}
//由于最后stack中必定存在一个递增序列,因为在最后一次s.push(i)之后,没有计算,所以要将此递增序列计算完
while(!s.isEmpty()) {
int cur = s.pop();
ma = Math.max(ma, heights[cur] * (s.isEmpty() ? heights.length : (heights.length - s.peek() - 1)));
}
return ma;
}
84.Largest Rectangle in histogram---stack的更多相关文章
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- 刷题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 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【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 (Array, Stack; DP)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 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 ...
随机推荐
- 学习Python最好的方法就是实践和教程并行,以下有一些资源和教程,还有一些学习思维导图:
1.Python 的 14 张思维导图下载地址: https://woaielf.github.io/2017/06/13/python3-all/ 2.Python基础教程|菜鸟教程: http:/ ...
- 【JavaScript&jQuery】轮展图
用bootstrap实现轮展图和用Jquery自定义轮展图两种 1.使用bootstrap插件 效果图: 用一个超简单的方法,那就是用bootstrap的插件,什么?不懂bootstrap?没关系 ...
- 洛谷 P2055 [ZJOI2009]假期的宿舍
洛谷 P2055 题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 C 来看B,C ...
- Android ActionBar 使用详解
ActionBar取代了以前的TitleBar,是一种更加灵活的人机交互方式:ActionBar并不是完全自立门户的一个新兴的东西,而是和3.0以下版本的menu进行了合并整合:so,添加action ...
- C++之正则表达式20171121
准确来说,不论在C++或C中,只要在Linux系统中都可以使用本文讲诉的正则表达式使用方式. 一.Linux中正则表达式的使用步骤: 编译正则表达式 regcomp() 匹配正则表达式 regexec ...
- 2018-2019 ACM-ICPC 沈阳赛区 K. Let the Flames Begin
K. Let the Flames Begin 题目链接:https://codeforces.com/gym/101955/problem/K 题意: n个人围成一个圈,然后依次从1开始报数,报到k ...
- python并行编程学习之并行计算存储体系结构
基于指令和可被同时处理的存储单元的数目,计算机系统可以分为以下四种类目: 单指令,单数据单元(SISD)在该体系结构中,计算机是单处理器机器,一次只能用单一的指令来操作单一的数据流.在SISD中,机器 ...
- C++11中对容器的各种循环遍历的效率比较
#include "CycleTimeTst.h" #include <string> #include <vector> #include <lis ...
- bzoj 1513 POI2006 Tet-Tetris 3D 二维线段树+标记永久化
1511: [POI2006]OKR-Periods of Words Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 351 Solved: 220[S ...
- scp 从本地往线上传文件
scp /home/wwwroot/default/tf_ment.sql root@IP:/home/wwwroot/default/