一问题描述


Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given height = [2,1,5,6,2,3],
return 10.

简单的翻译,就是求给的一连串长矩形的最大面积

二解决算法


本题解法很多,由于最近在学数据结构,故尝试用栈解决此题。

基本算法步骤:

1.将max_area置为0,声明一个栈s,s为存储对应方块的下标,且s为递增栈,具体做法将在下面继续说明

2.若栈为空或者当前矩形高度大于s的头元素对应的矩形高度,则其下标入栈,否则出栈,并计算出栈元素对应矩形高度的面积,更新max_area,直至当前头元素对应矩形高度小于将要入栈的矩形。

3.若最后栈非空,则将栈中元素一一出栈,并计算相应的面积,更新max_area。

4.最后的max_area为所求。

三算法原理说明


1.关于步骤2中出栈元素求其面积:

  由于栈s为递增栈,故当当前矩形高度小于s中头元素(设为i)对应的矩形高度时,s出栈,出栈后s(若不空)的头元素设为top1,则出栈矩形对应的宽度为(i - top1 - 1),画个图更便于理解,注意s空时,应为i。

2.关于步骤3中出栈元素求其面积:

对应于s出栈元素对应矩形的宽度应为size - top1 - 1(栈不空,size为整个输入矩形的宽度,top1同上定义),注意若栈空,则宽度为size。

四代码示例


  int largestRectangleArea(vector<int>& height) {
  int maxArea =0, top = 0, top1 = 0;
  int size = height.size();
  stack<int> sInt;
  if(!height.empty())
  {
    for(int i = 0; i < size; i++)
    {
      if(sInt.empty() || height[sInt.top()] < height[i])
        sInt.push(i);
      else
      {
        if(!sInt.empty())
        top = sInt.top();
        sInt.pop();
        if(!sInt.empty())
        top1 = sInt.top();
        int width = sInt.empty()? i: i - top1 - 1;
        maxArea = max(maxArea, height[top] * width);
        i--;
      }
    }
  }

  //栈非空
  while(!sInt.empty())
  {
    top = sInt.top();
    top1 = 0;
    sInt.pop();
    if(!sInt.empty())
    top1 = sInt.top();
    int width = (sInt.empty())? size: (size - top1 - 1);
    maxArea = max(maxArea, height[top] * width);
  }
  return maxArea;

用栈解决Largest Rectangle问题的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  3. leetcode Largest Rectangle in Histogram 单调栈

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

  4. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  5. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  6. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  7. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  8. Largest Rectangle in a Histogram POJ - 2559 (单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  9. poj2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

随机推荐

  1. Graded Browser Support

    ( The YUI Target Environments Matrix is here) About the Browser Test Baseline and Operating Systems ...

  2. python之路-Day8

    抽象接口 class Alert(object): '''报警基类''' def send(self): raise NotImplementedError class MailAlert(Alert ...

  3. 5.Struts2中的拦截器

    拦截器是Struts2中的核心,其自带很多很多的拦截器,这里主要介绍一下自定义拦截器,恩多一半情况下呢?我们不需要使用到自定义的拦截器,Struts2本身已经提 供了很多的拦截器供我们使用,对于自定义 ...

  4. C#批量插入数据到Sqlserver中的三种方式

    本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...

  5. thinkphp 验证

    //YongHuModel.class.php namespace Home\Model; use Think\Model; class YongHuModel exstends Model { pr ...

  6. UML(统一建模语言)

    需求分析阶段 用例图 定义:用例图并不是用来描述用例的.用例图的主要作用是:直观地描述系统对外提供的功能. 用例图的三个要素:角色.系统.用例 用例图的关系: 角色和用例的关系:有关和无关 用例和用例 ...

  7. Android 7.0 UICC 分析(四)

    本文讲解SIMRecords /frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/SIMRecords.jav ...

  8. 程序中保存状态的方式之ViewState

    程序中保存状态的方式有以下几种: 1.Application 2.Cookie 3.Session 4.ViewState:ViewState是保存状态的方式之一,ViewState实际就是一个Hid ...

  9. C入门---位运算

    程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算直接对整数在内存中的二进制位进行操作.由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快. (1),与(&)运算 ...

  10. JS之函数表达式

    度过一个愉快短暂的周末,又可以开始学习了!我爱学习,学习使人进步.今天学习函数表达式,着重学习下闭包函数. 函数表达式 可以在定义的函数声明之前调用它,但是不能在定义函数表达式之前调用它 /** * ...