题目链接: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;
}

法四:分治法,见https://leetcode.com/problems/largest-rectangle-in-histogram/discuss/28910/Simple-Divide-and-Conquer-AC-solution-without-Segment-Tree

84.Largest Rectangle in histogram---stack的更多相关文章

  1. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  2. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

  3. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  4. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  5. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  6. 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 ...

  7. 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 ...

  8. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  10. 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 ...

随机推荐

  1. http2.0可行性研究

     一.http2比http1有了更多新特性 1.使用了多路复用的技术,并发量支持比http1大几个数量级: 2.二进制分帧,改善网络延迟情况,提高传输速率: 3.支持header的数据压缩,数据体积变 ...

  2. java.lang - 不用import

    java.lang包是java语言的核心,它提供了java中的基础类.包括基本Object类.Class类.String类.基本类型的包装类.基本的数学类等等最基本的类.我们介绍一下Java 8中的j ...

  3. 【bzoj3811】【清华集训2014】玛里苟斯

    3811: 玛里苟斯 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 500  Solved: 196[Submit][Status][Discuss] ...

  4. python基础---- __getattribute__----__str__,__repr__,__format__----__doc__----__module__和__class__

    目录: 一. __getattribute__ 二.__str__,__repr__,__format__ 三.__doc__ 四.__module__和__class__ 一. __getattri ...

  5. 【字符串】KMP字符串匹配

    百度百科 Definition \(KMP\)算法是一个字符串匹配算法.他接收两个字符串\(A,B\),返回\(B\)在\(A\)中出现的所有位置. 以下称需要被匹配的串\(A\)为主串,可能在主串中 ...

  6. SAS数据步与过程步,数据步语句

    SAS数据步与过程步,数据步语句http://www.biostatistic.net/thread-2045-1-1.html  ---转载---原文作者:biostar(出处: 生物统计家园) 数 ...

  7. Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

  8. mysql 自动记录数据最后修改时间

    原文 -- mysql ,还真有这样的说法: mysql> create table test( ), -> uptime timestamp on update current_time ...

  9. Turkey HSD检验法/W法

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  10. Netty实例

    Netty是基于JDK NIO的网络框架 简化了NIO编程, 不用程序自己维护selector, 将网络通信和数据处理的部分做了分离 多用于做底层的数据通信, 心跳检测(keepalived) 1. ...