题目链接: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. [二十三]SpringBoot 之 redis

    本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...

  2. jQuery多重事件绑定

    1. <a> a标签默认绑定了一个onclick事件,当自己再写一个onclick事件的时候,默认自己写的那个优先执行. 如下程序,先执行(123),然后再发生跳转. <!DOCTY ...

  3. 【Codeforces 98E】 Help Shrek and Donkey 游戏策略神题

    from http://www.cnblogs.com/MashiroSky/p/6576398.html A君有n张牌,B君有m张牌,桌上还有一张反扣着的牌,每张牌都不一样. 每个回合可以做两件事中 ...

  4. MySQL数据库无法远程连接的解决办法

    远程登陆数据库的时候出现了下面出错信息: ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx', 经过今天下午的 ...

  5. Splay 的区间操作

    学完Splay的查找作用,发现和普通的二叉查找树没什么区别,只是用了splay操作节省了时间开支. 而Splay序列之王的称号可不是白给的. Splay真正强大的地方是他的区间操作. 怎么实现呢? 我 ...

  6. Work at DP

    转载请注明出处:http://www.cnblogs.com/TSHugh/p/8858805.html Prepared: (无notes的波兰题目的notes见我的波兰题目补全计划)BZOJ #3 ...

  7. Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分

    A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...

  8. Codeforces 895.D String Mark

    D. String Mark time limit per test 4 seconds memory limit per test 256 megabytes input standard inpu ...

  9. 洛谷P2563 [AHOI2001]质数和分解

    题目描述 任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质数之和表达式(包括只有一个数构成的和表达式的情况),并且可能有不止一种质数和的形式.例如,9 的质数和表达式就有 ...

  10. virt-manager管理器新建虚拟机时出错:unsupported format character

    启动管理器出错:unsupported format character ‘��0xffffffef) at index 30 经验证,解决办法如下: 1.获取virt-manager的rpm包,并强 ...