题目链接: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. android面试(1)----布局

    1.说出android 五中布局,并说出各自作用? FrameLayout: 堆叠布局,也是就可以堆在一起.最长应用于Fragment的使用上. LinearLayout: 线性布局,可以是竖排或水平 ...

  2. IDEA使用switch传入String编译不通过

    今天在使用IDEA的时候,用到switch分支语句,传入String参数的时候一直报错,下面是源码报错截图: 看错误提示并没有提到switch支持String类型,不过ava1.7之后就支持Strin ...

  3. 【数据库_Mysql】Mysql知识汇总

    1.将多列字段合并显示用CONCAT(XX,XX,...): 2.查询表中某字段重复的数据: 查重复字段:select 字段 from table group by 字段 having count(* ...

  4. [十二]SpringBoot 之 servlet

    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用spring-Boot时,嵌 ...

  5. C++11线程使用总结

    std::thread 在 <thread> 头文件中声明,因此使用 std::thread 需包含 <thread> 头文件. <thread> 头文件摘要 &l ...

  6. 洛谷 P1278 单词游戏 【状压dp】

    题目描述 Io和Ao在玩一个单词游戏. 他们轮流说出一个仅包含元音字母的单词,并且后一个单词的第一个字母必须与前一个单词的最后一个字母一致. 游戏可以从任何一个单词开始. 任何单词禁止说两遍,游戏中只 ...

  7. Linux用户创建及权限管理

    作业一: 1,新建用户natasha,uid为1000,gid为555,备注信息为“master” useradd natasha            vim /etc/passwd         ...

  8. 第2章-Vue.js指令

    一.学习目标 了解 什么 是 Vue.js 指令 理解 Vue.js 指令的 用途 掌握 Vue.js 指令的书写规范 能够 使用 Vue.js 指令完成部门页面交互效果(难点和重点) 二.指令的基本 ...

  9. linux shell学习二

    参考:http://www.cnblogs.com/waitig/p/5531463.html Shell注释 Shell中的注释以“#”号开头,所有以“#”号开头的代码都会被解释器所忽略. 比如下面 ...

  10. Java设计模式の模版方法模式

    概述 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的 ...