一问题描述


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. 阿里云SLB后Nginx、Tomcat获取真实IP

    一.SLB后Nginx如何获取真实IP 前提:nginx作为slb获取真实ip是使用 http_realip_module,默认一键安装包安装的nginx没有安装这个模块需要重新重新编译nginx并加 ...

  2. js中var self=this的解释

    每个函数在定义被ECMAScript解析器解析时,都会创建两个特殊的变量:this和arguments,换句话说,每个函数都有属于自己的this对象,这个this对象是在运行时基于函数的执行环境绑定的 ...

  3. 事件:卸载事件(onunload)

    这是几点应卸载事件的说明 ①目前试了Firefox.Google Chrome.IE三个浏览器,该事件只对IE起作用. ②onunload事件对于刷新页面和超链接跳转其他页面情况有效,对于关闭页面无效 ...

  4. 在svg中的line和path根据路径返回x,y

    由于path有自带的api可获得总长度,和某个长度返回的坐标. var total = d.path.getTotalLength();//返回总长度 var point = d.path.getPo ...

  5. C# 委托和事件 与 观察者模式(发布-订阅模式)讲解 by天命

    使用面向对象的思想 用c#控制台代码模拟猫抓老鼠 我们先来分析一下猫抓老鼠的过程 1.猫叫了 2.所有老鼠听到叫声,知道是哪只猫来了 3.老鼠们逃跑,边逃边喊:"xx猫来了,快跑啊!我是老鼠 ...

  6. [html]LESS-1.3.3

    网站:http://www.bootcss.com/p/lesscss/ 下载链接:http://files.cnblogs.com/files/z5337/less-1.3.3.min.js

  7. oracle去掉重复记录语句

    oracle去掉重复记录语句   比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完全相同的记录查询出来 select p1.*   from persons  p1 ...

  8. dbcp数据源配置杂谈

    <!-- 数据源1 --> #驱动信息(driver, url, username, password)driverClassName=net.sourceforge.jtds.jdbc. ...

  9. HTML5离线篇收藏--- cache manifest

    自从翻译了<解读 HTML5:建议.技巧和技术>,就一直没有时间去看 HTML5 相关的东西.上周一次偶然的工作间隙折腾了下 Cache Manifest .当时直接拿博客当测试环境,虽然 ...

  10. NGUI 使用EventDelegate.Add与UIInput.onSubmit、UIInput.onChange限定编辑框中的内容

    Unity中,使用NGUI,通常为某个控件(如按钮)绑定事件(单击.双击.拖拽.滚轮.选择等)都是用UIEventListener,比如: public void Bind() { UIEventLi ...