上一篇文章讲了该题的一个解法。后来又发现一个更好的解法。

首先依旧考虑一个升序的数列,例如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 解法二的更多相关文章

  1. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

  2. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  3. Largest Rectangle in Histogram及二维解法

    昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...

  4. LeetCode: Largest Rectangle in Histogram 解题报告

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

  5. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

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

  6. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  7. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  8. [LeetCode] Largest Rectangle in Histogram

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

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

随机推荐

  1. SQL server基本操作(一)

      --1.create database CREATE DATABASE MyDB GO --2.use database USE MyDB GO --3.create table CREATE T ...

  2. C++ 四则运算简单设计

    如果说要用C++写一个简单的四则运算的程序,相信难不到人,这还不简单吗?然后用不了五分钟,三下五除二,就出了下面的代码,一调试,没问题..... #include <iostream> u ...

  3. IPC with pipes, demo of 'popen'

    #include <stdio.h> #include <unistd.h> int main() { FILE* stream = popen ("sort&quo ...

  4. iOS Foundation框架 -2.常用集合类简单总结

    Foundation框架中常用的类有:NSString.NSArray.NSSet.NSDictionary 以及它们对应的子类 NSMutableString.NSMutableArray.NSMu ...

  5. gcc常用选项

    gcc选项:    -c         只编译,不链接成为可执行文件,编译器只是由输入的.c等源代码文件生成.o为后缀的目标文件,通常用于编译不包含主程序的子程序文件.    -std=     指 ...

  6. WPF学习笔记 控件篇 属性整理【1】FrameworkElement

    最近在做WPF方面的内容,由于好多属性不太了解,经常想当然的设置,经常出现自己未意料的问题,所以感觉得梳理下. ps:先补下常用控件的类结构,免得乱了 .NET Framework 4.5 Using ...

  7. Basic Vlan Concepts

    1.  Vlan Benefit ·To reduce CPU overhead on each device by reducing the number of devices that recei ...

  8. 【转】微软MVP攻略 (如何成为MVP?一个SQL Server MVP的经验之谈)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 初衷 什么是微软MVP? 成为微软MVP的条件? 如何成为微软MVP? (一) 申请时间划分 (二) 前期准备 (三) ...

  9. 说说iOS中的手势及触摸

    一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResp ...

  10. C# 实现Oracle中的数据与Excel之间的转换

    最近项目要求实现数据库之间数据在各个数据库之间导入导出,在此做个笔记 1. 将Oracle中的表导入到Excel中,反之亦然  private static readonly string conne ...