题目

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、如果已知height数组是升序的,应该怎么做?

比如1,2,5,7,8

那么就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)

也就是max(height[i]*(size-i))

2、使用栈的目的就是构造这样的升序序列,按照以上方法求解。

但是height本身不一定是升序的,应该怎样构建栈?

比如2,1,5,6,2,3

(1)2进栈。s={2}, result = 0

(2)1比2小,不满足升序条件,因此将2弹出,并记录当前结果为2*1=2。

将2替换为1重新进栈。s={1,1}, result = 2

(3)5比1大,满足升序条件,进栈。s={1,1,5},result = 2

(4)6比5大,满足升序条件,进栈。s={1,1,5,6},result = 2

(5)2比6小,不满足升序条件,因此将6弹出,并记录当前结果为6*1=6。s={1,1,5},result = 6

2比5小,不满足升序条件,因此将5弹出,并记录当前结果为5*2=10(因为已经弹出的5,6是升序的)。s={1,1},result = 10

2比1大,将弹出的5,6替换为2重新进栈。s={1,1,2,2,2},result = 10

(6)3比2大,满足升序条件,进栈。s={1,1,2,2,2,3},result = 10

栈构建完成,满足升序条件,因此按照升序处理办法得到上述的max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10

综上所述,result=10

AC代码

 class Solution {
public:
/*方法一:每个坐标点左右延伸(当高度降低停止延伸)构造矩形,但是大集合TLE*/
int largestRectangleArea1(vector<int> &height) {
if (height.empty())
return ;
int maxArea = ;
int len = height.size();
for (int i = ; i < len; ++i)
{
/*记录包含第i个柱体的矩形面积*/
int tmpArea = height[i];
int left = i - , right = i + ;
/*左侧扩展*/
while (left >= && height[left] >= height[i])
{
tmpArea += height[i];
--left;
}//while
/*右侧扩展*/
while (right < len && height[right] >= height[i])
{
tmpArea += height[i];
++right;
}//while if (maxArea < tmpArea)
maxArea = tmpArea;
}//for return maxArea;
}
/*方法二:利用栈*/
int largestRectangleArea(vector<int> &height) {
if (height.empty())
return ;
stack<int> stk;
int len = height.size();
int maxArea = ;
for (int i = ; i < len; i++)
{
if (stk.empty() || stk.top() <= height[i])
stk.push(height[i]);
else
{
int count = ;
while (!stk.empty() && stk.top() > height[i])
{
count++;
maxArea = max(maxArea, stk.top()*count);
stk.pop();
}
while (count--)
stk.push(height[i]);
stk.push(height[i]);
}//else
}//for
int count = ;
while (!stk.empty())
{
maxArea = max(maxArea, stk.top()*count);
stk.pop();
count++;
}//while
return maxArea;
}
};

GitHub测试程序源码

LeetCode(84) Largest Rectangle in Histogram的更多相关文章

  1. (数组)Largest Rectangle in Histogram(栈解问题)

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

  2. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  3. leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle

    https://leetcode.com/problems/largest-rectangle-in-histogram/ https://leetcode.com/problems/maximal- ...

  4. LeetCode(84): 柱状图中最大的矩形

    Hard! 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度 ...

  5. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...

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

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

  7. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

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

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

  9. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram

随机推荐

  1. c# 高效率导出多维表头excel

    [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThread ...

  2. 卡特兰数 (Catalan)

    卡特兰数:(是一个在计数问题中出现的数列) 一般项公式: 1.         或       2.   递归公式: 1.  或 2. 注:全部可推导. (性质:Cn为奇数时,必然出现在奇数项 2k- ...

  3. python3实现简单爬虫功能

    本文参考虫师python2实现简单爬虫功能,并增加自己的感悟. #coding=utf-8 import re import urllib.request def getHtml(url): page ...

  4. link

    public IEnumerable InsuranceSearch(InsuranceSC sc, out int TotalCount) { var data = from q in Insura ...

  5. 初始化成员列表 ——— 类的const成员和引用成员的初始化

    class A { public: A(){}; const int num; CString& s; } A::A() { cout<<A con<<endl; } ...

  6. 二模02day1解题报告

    T1.淘汰赛制 比赛时的淘汰赛制,给出每两个球队比赛的胜率,求出最终胜率最高的队伍. 这题的概率真的很难算啊感觉...一开始打的代码打下来就是用f[i][j]表示i场比赛后第j人还在场的概率.不难看出 ...

  7. TCP/IP 之大明王朝邮差

    本系列文章全部摘选自"码农翻身"公众号,仅供个人学习和分享之用.文章会给出原文的链接地址,希望不会涉及到版权问题. 个人感言:真正的知识是深入浅出的,码农翻身" 公共号将 ...

  8. IO同步、异步与阻塞、非阻塞

    一.同步与异步同步/异步, 它们是消息的通知机制 1. 概念解释A. 同步所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回. 按照这个定义,其实绝大多数函数都是同步调用(例如si ...

  9. windows7下安装python3的scrapy框架

    强大的Anaconda和Spyder.不过如何在这个平台上安装Scrapy呢. 打开MS-DOS(win+R输入cmd回车) 然后输入: conda install -c scrapinghub sc ...

  10. Mysql忘记用户密码的解决办法

    1.1 忘记用户密码的解决办法 普通用户,直接用root超级管理员登录进去修改密码就可以了,但是如果root密码丢失了,怎么办呢? 1.1.1 msyqld_saft方式找回密码 停止mysql:se ...