84. Largest Rectangle in Histogram

题目

 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].

![](https://images2018.cnblogs.com/blog/864046/201804/864046-20180410194946191-934507527.png)

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

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

解析

直接的暴力的思路就是对于每一组子数组,找到其中最低的高度,然后求面积,进而求出最大的矩形面积。总共有n2个子数组,找最低高度是O(n)的操作,所以复杂度是O(n3)。

进一步,可以从每一个bar往两边走,以自己的高度为标准,直到两边低于自己的高度为止,然后用自己的高度乘以两边走的宽度得到矩阵面积。因为对于任意一个bar都计算了以自己为目标高度的最大矩阵,所以最好的结果一定会被取到。每次往两边走的复杂度是O(n),总共有n个bar,所以时间复杂度是O(n^2)。

最后说说最优的解法,思路跟Longest Valid Parentheses类似,主要维护一个栈,这个栈从低向上的高度是依次递增的。如果遇到当前bar高度比栈顶元素低,那么就出栈直到栈顶元素低于当前bar,出栈过程中检测前面满足条件的矩阵:

如果栈已经为空,说明到目前为止所有元素(当前下标元素除外)都比出栈元素高度要大(否则栈中肯定还有元素),所以矩阵面积就是高度乘以当前下标i。
如果栈不为空,那么就是从当前栈顶元素的下一个到当前下标的元素之间都比出栈元素高度大(因为栈顶元素第一个比当前出栈元素小的),所以矩阵面积就是高度乘以当前下标i减栈顶元素再减1。

注意最后还要对剩下的栈做清空并且判断,因为每次是对于前面的元素面积进行判断,所以循环结束中如果栈中仍有元素,还是要继续判断面积直到栈为空。

时间复杂度:O(n)

空间复杂度:O(1)


// 84. Largest Rectangle in Histogram
class Solution_84 {
public:
int largestRectangleArea(vector<int>& heights) { int res = 0;
stack<int> st; //存储递增的下标
for (int i = 0; i < heights.size();i++)
{
while (!st.empty() && heights[st.top()]>heights[i]) //出栈操作,之前都是递增的
{
int h = heights[st.top()];
st.pop(); if (st.empty())
{
res = max(res, h*i);
}
else
{
res = max(res, h*(i - st.top()-1)); //当前区间[st.top+1,i-1]
} }
st.push(i);
} while (!st.empty()) //递增的
{
int h = heights[st.top()];
st.pop();
int s = h * (st.empty() ? heights.size() : (heights.size() - st.top() - 1));
res = max(res, s);
} return res;
}
};

题目来源

84. Largest Rectangle in Histogram-hard的更多相关文章

  1. 84. Largest Rectangle in Histogram

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

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

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

  3. 刷题84. Largest Rectangle in Histogram

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

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

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

  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. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

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

  8. [LeetCode#84]Largest Rectangle in Histogram

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

  9. LeetCode OJ 84. Largest Rectangle in Histogram

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

  10. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

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

随机推荐

  1. spring_150906_sqlmapclientdaosupport_getSqlMapClientTemplate

    添加到ibatis相关jar包! 实体类: package com.spring.model; public class DogPet { private int id; private String ...

  2. HDU 6188 Duizi and Shunzi

    栈. 将数字排序后,一个一个压入栈.如果栈顶两个元素形成了对子,那么$ans+1$,弹出栈顶两个元素:如果栈顶三个元素形成了顺子,那么$ans+1$,弹出栈顶三个元素. #include<bit ...

  3. EOJ 3247 铁路修复计划

    二分,最小生成树. 二分一下$k$,然后每次算最小生成树验证即可,事实证明,$cmp$函数,参数用引用还是能提高效率的,不引用一直$TLE$,时限有点卡常. 然后错误的代码好像$AC$了啊,$L$和$ ...

  4. 【小思考】Python里面有声明和定义分离这一说么?

    第一部分: 探究这个问题,还是因为编程的时候碰到了这个错误: 提示tcplink没有定义,tcplink是我自己写的一个给监听到的tcp连接请求分配新线程的函数,不过是写在了下面,就像这样: 如果是C ...

  5. 【Java】SpringBoot入门学习及基本使用

    SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...

  6. Openstack关于Regions和Availability Zones

    在AWS中有Region和Availability Zones的概念,并且在openstack中也实现了两者,只是不太容易看出来. 此文主要介绍他们的概念和关系,以及在openstack中的实现. 如 ...

  7. HDU 6136 Death Podracing(循环链表)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6136 [题目大意] 一堆人在操场上跑步,他们都有一定的速度和初始位置, 当两个人相遇的时候编号较小 ...

  8. 记一次初步Linux提权

    前言. 提权这么久了  还是头一次提下Linux的服务器... 由于之前一直钻研的win服务器  要不是前些日子爆出来Struts2-045漏洞 估计还没时间接触Linux提权.... 正文. st2 ...

  9. [COGS2426][HZOI 2016]几何

    [COGS2426][HZOI 2016]几何 题目大意: 给定平面坐标系内\(n\)个整点,求这些整点能构成的正多边形的边数的最大值. 思路: 一个基本结论:平面直角坐标系内能够形成的正多边形一定是 ...

  10. 1、Redis简介、安装和基础入门

    -------------------------------------------------------- 主要内容包括: 1.Redis简介 2.Redis安装.启动.停止 3.Redis基础 ...