一问题描述


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. java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0

    使用repalceAll 方法出现java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0异常 ...

  2. spring mvc 定时器

    1.下载quartz-all-1.7.3.jar包 a.在Spring配置和Quartz集成内容时,有两点需要注意 b.在<Beans>中不能够设置default-lazy-init=&q ...

  3. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

  4. quartz 框架定时任务,使用spring @Scheduled注解执行定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...

  5. MVC 基础和增删改、登录

    一.什么是MVC?1.了解MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:       Model(模型)表示应用程序核心 ...

  6. 关于git中git pull --rebase中的一些坑

    在公司里面,每次我更改完代码,准备pull最新代码时,总是会遇到各种各样的问题.,因为对应的问题不同,解决方法很多.但是比较通用的办法还是有的: git pull --rebase   //报错时 g ...

  7. nullcon HackIM 2016 -- Crypto Question 1

    You are in this GAME. A critical mission, and you are surrounded by the beauties, ready to shed thei ...

  8. js中的原形链问题

    ---恢复内容开始--- 一.在js中大家讨论的原形链都是围绕在prototype和__proto__. 1.__proto__是内部原型 2.prototype是构造器原型(构造器就是构造函数) 3 ...

  9. CSS布局中的水平垂直居中

    CSS布局中的水平垂直居中 各位好,先说两句题外话.今天是我开通博客园的博客第一天,虽然我申请博客园的账号已经有一年半了,但是由于各种原因迟迟没有开通自己的博客.今天非常有幸开通博客,在此也写一篇关于 ...

  10. LLVM example for main

    #include "llvm/IR/CallSite.h" #include "llvm/IR/Instruction.h" #include "ll ...