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.

法I: 使用动态规划

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int* l = new int [height.size()]; //状态:向左连续的>=height[i]的最后一个位置
int* r = new int [height.size()]; //状态:向右连续的>=height[i]的最后一个位置 l[0]=0;
for( int i =1; i <height.size(); i++) //从左往右扫描
{
if(height[i]==0)
{
l[i] = 0;
continue;
}
int j = i-1;
for(; j >= 0; j--){
if(height[i] <= height[j]) j = l[j]; //用动态规划求l[i]否则会Time Limit Exceeded
if(height[i] > height[j]) break;
}
l[i] = j+1;
}
r[height.size()-1]=height.size()-1;
for(int i =height.size()-2; i >=0; i--){ //从右往左扫描
if(height[i]==0)
{
r[i] = 0;
continue;
}
int j = i+1;
for(; j <height.size(); j++){
if(height[i] <= height[j]) j = r[j]; //动态规划
if(height[i] > height[j]) break;
}
r[i] = j-1;
} int area;
int largestArea = 0;
for(int i = 0; i< height.size(); i++)
{
area = (r[i]-l[i]+1) * height[i];
if(area>largestArea) largestArea = area;
}
return largestArea;
}
};

法I的空间负责度是O(n)+O(n), 为了节省空间,可以用stack代替array来存储最高高度的位置。

法II: stack的栈顶元素表示下一个用来计算面积的高度。

进栈条件:当前元素比栈顶元素大或相等, 进栈后 i++

出栈条件:当前元素比栈顶元素小,出栈后 计算面积 i不变(每个元素都要进一次栈,i要等到比自己小的栈顶元素,然后进栈)

为什么要在此时计算面积?

这个高度到i位置截至了,无法往有再延伸,所以要在当下计算面积。

如何界定计算面积时的左界和右界?

  • 左界=新栈顶元素+1
  • 右界=当前位置-1

为什么可以这样?

从新栈顶到i的元素高度都>=height[idx],因为如果其中有<height[idx]的元素,那么idx早就出栈了。

空间复杂度最坏情况也只有O(n)

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
if(height.size() == ) return ; int res = ;
stack<int> idxStack;
height.push_back(); //为了如果在最后height高于栈顶元素,能够再进一次else,把stack中的元素pop出来计算 int i = ;
int idx;
int width;
while(i < height.size())
{
if(idxStack.empty() || height[i] >= height[idxStack.top()]){ //当前高度>=栈顶高度
idxStack.push(i); //入栈
i++;
}
else{ //高度降低了
idx = idxStack.top();
idxStack.pop(); //出栈
width = idxStack.empty() ? i : (i-idxStack.top()-); //界定左右边界
res = max(res, height[idx] * width); //计算面积,并更新最大面积
}
}
height.pop_back();
return res;
}
};

84. Largest Rectangle in Histogram (Array, Stack; DP)的更多相关文章

  1. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  2. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

  3. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  4. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  5. 【LeetCode】84. Largest Rectangle in Histogram

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

  6. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

  7. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  8. 84. Largest Rectangle in Histogram(直方图最大面积 hard)

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

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

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

随机推荐

  1. python-并发测试用例

    以前看了虫师的并发,然后觉得以后如果遇上领导要求一个模块里的并发怎么办,然后就想到了下面的方法: 代码: 在原有的基础下再往casedir数组加模块三里面细分的对象.(这里可以封装成函数调用,工作需要 ...

  2. Spring Cloud config之二:功能介绍

    SVN配置仓库 示例见:http://lvdccyb.iteye.com/blog/2282407 本地仓库 本地文件系统 使用本地加载配置文件.需要配置:spring.cloud.config.se ...

  3. C++ 什么叫做离散化

    C++ 什么叫做离散化 如果说今年这时候OIBH问得最多的问题是二分图,那么去年这时候问得最多的算是离散化了.对于“什么是离散化”,搜索帖子你会发现有各种说法,比如“排序后处理”.“对坐标的近似处理” ...

  4. crontab 执行脚本,报错/home/scripts/eyeMonitor.sh: line 8: node: command not found

     报错现象:在shell下执行node没有任何问题,但crontab自动运行就会报错. 原因:node的安装路径:/root/.nvm/versions/node/v6.7.0/bin/node Sh ...

  5. centos-linux热拔插scsi硬盘

    自己配置虚拟机,需要添加一块虚拟硬盘存放数据.虚拟机在更新软件,不想停机.学习了下热拔插硬盘的知识点 1. 在虚拟机中创建虚拟磁盘并添加. 2. 查看目前的磁盘信息cat /proc/scsi/scs ...

  6. 联想Z510升级BCM94352HMB刷网卡白名单曲折经历

    联想Z510笔记本:CPU I7 4702MQ没毛病 :内存4G DDR3不上虚拟机办公足够用: 硬盘升级为SSD240G足够用:有线网卡100M,真是垃圾,不过有线网卡是主板上的芯片,这个我可动不了 ...

  7. 6.15-初识JSP、javaweb

    一.javaweb web服务器 tomcat C/S 客户端/服务器 B/S 浏览器/服务器 URL: http协议 https 加密的协议 localhost 127.0.0.1 常用web服务器 ...

  8. 解决不能正常访问workerman的问题

    问题描述: 在阿里云ECS上部署了workerman的应用(ECS是专有网络),在ECS安全组里已经允许workerman需要的全部端口,但是外网一直不能正常打开(注,其他服务,比80端口外部是可以用 ...

  9. MySQL数据库InnoDB存储引擎

    MySQL数据库InnoDB存储引擎Log漫游  http://blog.163.com/zihuan_xuan/blog/static/1287942432012366293667/

  10. 使border处于边框内

    box-sizing需要指定高度,它在这个高度出现,不会增加额外的高度 .box{box-sizing: border-box;height: 64px;}