84. Largest Rectangle in Histogram (Array, Stack; DP)
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)的更多相关文章
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 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 ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- 84. Largest Rectangle in Histogram(直方图最大面积 hard)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- 杂项:mPaaS
ylbtech-杂项:mPaaS 1. 概述返回顶部 mPaaS 是源于支付宝 App 的移动开发平台,为移动开发.测试.运营及运维提供云到端的一站式解决方案,能有效降低技术门槛.减少研发成本.提升开 ...
- 使用postman模拟appium的http请求
Appium是Server,接收http请求,使用Postman模拟请求 1.anyproxy 1.1.安装和运行 #安装 npm i -g anyproxy # 运行anyproxy,端口默认800 ...
- 关于clearfix和clear的讨论
本文摘自百度文库 还是提到了一个关于网页制作很古老的问题,浮动的清除. 虽然看过一些资料介绍说能不用浮动就尽量不要用,但对定位不是很熟的我来说,浮动就不能不用了:既然惹上这个麻烦,就得想个办法进行解决 ...
- [UE4]蓝图转换成C++代码
版本:4.12 1.进行如下设置 2.将项目打包出来(任意一平台都行,本文以Windows为例) 3.打包完成后才会在原项目工程中生成蓝图转换成c++的代码 4.如图路径(转换后的代码路径较深所以一步 ...
- phpstorm使用教程
phpstorm包含了webstorm的全部功能,更能够支持php代码.PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时 ...
- skopt超参数优化实例
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_boston from skl ...
- ie6,7下的textarea的type获取
<input type='button' value="按钮" class='gys'> <textarea class='gys gystextarea'> ...
- Python - Django - App 的概念
App 方便我们在一个大的项目中,管理实现不同的业务功能 创建 App: 命令行: python manage.py startapp app名 使用 Pycharm 创建: 文件 -> 新建项 ...
- MYSQL体系结构-来自期刊
MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...
- centos7防火墙使用方法
参考网站:https://blog.csdn.net/achang21/article/details/52538049 添加开放指定端口: [root@yao bin]# firewall-cmd ...