leetcode Largest Rectangle in Histogram 解法二
上一篇文章讲了该题的一个解法。后来又发现一个更好的解法。
首先依旧考虑一个升序的数列,例如1,2,3,4,5。那么它的最大矩形显然是有5种可能,即
1*5,2*4,3*3,4*2,1*5。所以最大的矩形为9。那么显然不可能是升序的数列。
依据以下几条规则对其进行处理。
有栈stack和待处理数组a[n]
1.如果stack为空,那么将a[i]入栈。
2.如果a[i]>=stack.peek(),那么将a[i]入栈
3.如果a[i]<stack.peek(),那么stack弹出,直到a[i]>=stack.peek()。对于所有的弹出值,计算其面积。
执行完弹出操作之后,再压入与弹出数目相同的a[i]
4.遍历完a[i]之后,再对stack中的元素进行处理。
给出一个例子:
比如2,1,5,6,2,3
(1)2进栈。s={2}, result = 0
(2)1比2小,不满足升序条件,因此将2弹出,并记录当前结果为2*1=2。
将2替换为1重新进栈。s={1,1}, result = 2
(3)5比1大,满足升序条件,进栈。s={1,1,5},result = 2
(4)6比5大,满足升序条件,进栈。s={1,1,5,6},result = 2
(5)2比6小,不满足升序条件,因此将6弹出,并记录当前结果为6*1=6。s={1,1,5},result = 6
2比5小,不满足升序条件,因此将5弹出,并记录当前结果为5*2=10(因为已经弹出的5,6是升序的)。s={1,1},result = 10
2比1大,将弹出的5,6替换为2重新进栈。s={1,1,2,2,2},result = 10
(6)3比2大,满足升序条件,进栈。s={1,1,2,2,2,3},result = 10
栈构建完成,满足升序条件,因此按照升序处理办法得到上述的max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10
综上所述,result=10
仔细分析一下,其实这个解法的意思就是,如果是升序,那么直接计算它的面积,遇到了下降点,那么实际上
这个点是个凹陷,包含了这个点的矩形只能以这个点的值为高度。最后统计stack的目的就是统计下降点。
给出代码
import java.util.Stack;
public class Solution2 {
/**
* @param args
*/
public int largestRectangleArea(int[] height) {
Stack<Integer> stack=new Stack<Integer>();
int maxArea=0;
for(int h:height)
{
System.out.println(stack);
if(stack.empty()||stack.peek()<h)
{
stack.push(h);
}
else
{
int i=1;
while(!stack.empty()&&stack.peek()>h)
{
int tmp=stack.pop();
if(tmp*i>maxArea)
maxArea=tmp*i;
i++;
}
for(int j=0;j<i;j++)
{
stack.push(h);
}
}
}
System.out.println(stack);
int i=1;
if(stack.empty())
return maxArea;
int previous=stack.pop();
while(!stack.empty())
{
if(previous!=stack.peek())
{
System.out.println(previous*i+"ddd");
if(previous*i>maxArea)
maxArea=previous*i;
i++;
previous=stack.pop();
}
else
{
i++;
stack.pop();
}
}
if(previous*i>maxArea)
maxArea=previous*i;
return maxArea;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int []a={1,2,2};
System.out.println(new Solution2().largestRectangleArea(a));
}
}
leetcode Largest Rectangle in Histogram 解法二的更多相关文章
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- Largest Rectangle in Histogram及二维解法
昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetcode -- Largest Rectangle in Histogram TODO O(N)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- 构建前端Mock Server
写在前面 最开始只是在做活动页面时苦于效率太低制定了这样一个自动化的工作环境, 所以Github上项目名是Rapid-Dev-Activity-Page(快速开发活动页...). 活动页这类比较简单的 ...
- 《HTML5与CSS3基础教程》学习笔记 ——Two Day
第七章 1. 样式表:选择器和生命块 2. !important: 某条声明的重要程度比其他高,在末尾添加 3. 属性值:inherit; 是强制继承 4. 1em=16px; 5. 可以 ...
- Java并发编程概要
线程开的越多,则性能越好吗? 未必,影响多线程性能的因素有:上下文切换,竞争/死锁,资源限制等.对于这些因素要均衡考量,才能获得较好的性能. 并发控制/线程间的通信方式 基本的并发控制原语有 vola ...
- 3D球状标签云(兼容IE8)
看见一个很有趣的标签云,3D球状,兼容 IE 8,亲测可用!其他版本没有测试.觉得挺有意思就拿来记录下来,学习学习,本文下方会放出我看的文章地址,先看一下效果: 接下来是代码,html + css + ...
- DailyWallpaper v1.02 released
上次忘了写软件说明,先补上一个. 软件说明: 每天定时(暂定上午11点)下载美国国家地理网站的photo of the day图片作为桌面壁纸.下载图片会以日期为名称保存在C:\DailyWallpa ...
- 为什么ARM的frq中断的处理速度比较快
FRQ向量位于异常向量表的最末端,不需要跳转就可以直接执行后面跟随的异常处理程序:FRQ模式中私有寄存器数量最多,在进行异常处理时不需要对这些寄存器进行压栈保存.
- winform Config文件操作
using System;using System.Collections.Generic;using System.Text;using System.Xml;using System.Config ...
- nginx自启动脚本
#!/bin/bash # #Startup script for Nginx - this script starts and stops the nginx daemon # # chkconfi ...
- nignx+php-fpm环境下 phpmyadmin打开空白的原因探究
打开phpmyadmin一直是空白的,发现是js的问题,原因是pma的js/get_script_js.php读取js不完整 很容易的将问题原因想到了php的输出缓存大小上,我把php.ini里的ou ...
- 错误信息:未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序。
下载2007 Office system 驱动程序:数据连接组件安装 http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b ...