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 ...
随机推荐
- 会场安排问题—NYOJ14
时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工作就是安排学校 ...
- C#打开mdb文件,获取文件下的所有表格,以及获取表格下的所有字段
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aspxWeb2 ...
- debian终端菱形乱码修复
最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok
- functional javascript
(转载请注明出处!) 今早带我的master跟我分享了他最近看<functional javascript>一书的感悟,瞬间觉得写1w行代码都不如看本好书来的好啊! 于是在下午的写的项目中 ...
- 三角函数计算,Cordic 算法入门
[-] 三角函数计算Cordic 算法入门 从二分查找法说起 减少乘法运算 消除乘法运算 三角函数计算,Cordic 算法入门 三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来 ...
- div+css3实现的小丸子和爷爷
HTML代码 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- PHP实现根据浏览器跳转不同语言页面代码
以下是对使用PHP实现根据浏览器跳转不同语言页面的代码进行了介绍,需要的朋友可以过来参考下 代码: <?php /** * 根据不同浏览器跳转不同页面 * 来源:www.jbxue.com * ...
- Ajax入门小例子
大牛文章:http://www.cnblogs.com/guduoduo/p/3681296.html ---Ajax基础学习 http:/ ...
- haproxy 安装与配置文件详解
本文主要阐述haproxy的安装配置详解,对于它的概念,作用,功能,和其它LB软件的区别,优点,缺点等不再进行说明. 一. haproxy 的安装配置 # cat /etc/redhat-releas ...
- 万网域名解析到IP地址
进入https://home.console.aliyun.com/#/的阿里云控制台 再自己购买的域名列表里进行操作 添加一个A解析