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.

思路分析:暴力破解方法应该不需要多说,直接计算每一个元素所能构成的最大矩形面积,并与max比较并记录。

这道题,是看了网上的众多博客之后结合自己的理解来mark一下这个过程(最主要是一遍就解决了,时间复杂度O(n)):

  用到了栈,栈顶元素,跟当前索引是关键:

    入栈:如果栈为空或者栈顶元素(其在直方图中的对应高度)小于等于当前索引(对应高度),这样就使得,栈中的元素(对应高度)是非递减的。这样符合贪心算法的思想,因为若是当前索引(对应高度)大于栈顶元素(对应高度),则继续入栈,因为必然能得到更大的矩形面积,贪心!

    出栈:当前索引(对应高度)<栈顶元素(对应高度),则栈顶元素出栈,并记录下其对应高度,并计算其能对应的最大矩形面积。注意一个关键点:栈顶元素跟当前索引之间的所有元素(对应高度)都大于等于这两个端点对应高度!

 class Solution {
public:
int largestRectangleArea(vector<int>& height) {
stack<int> sta;
height.push_back();
int i = ,sum=;
while (i < height.size())
{
if (sta.empty() ||height[sta.top()] <= height[i])
{
sta.push(i++);
}
else
{
int t = sta.top();
sta.pop();
sum = max(sum, height[t]*(sta.empty()?i:i - sta.top() - ));
}
}
return sum;
}
};

largest rectangle in histogram leetcode的更多相关文章

  1. Largest Rectangle in Histogram leetcode java

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

  2. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  3. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  4. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

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

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

  6. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

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

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

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

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

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

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

随机推荐

  1. RDA PQ工具使用 (Adi Analysis)

    PQ工具“ColorAdjustTool.exe”,请注意芯片的选择: RDA512C选择533 RDA8501选择331 RDA8503选择131  工模菜单 COLOR LUT: R/G/B/Y/ ...

  2. Linux的终端类型

    终端是一个很重要的外设,用过终端设备的人都知道如果设备类型不对就会有乱字符,也可用仿真终端软件如netterm试验一下,Linux的终端信息放在 /usr/share/terminfo下,在这个目录的 ...

  3. bzoj1783

    博弈论+dp 从未做过博弈论... 思路是这样的,我们倒着考虑,分别设f[i]表示先手选了a[i]后能取得的最大值,g[i]表示先手取了a[i]后后手能获得的最大值 g[i]=f[mx],f[mx]是 ...

  4. 1-1 课程导学 & 1-2 项目需求分析,技术分解.

    1-1 课程导学 1-2 项目需求分析,技术分解. 要有一定的dart基础,了解安卓和ios的一些普通的开发

  5. bzoj 1082: [SCOI2005]栅栏【二分+dfs】

    二分答案,dfs判断是否可行,当b[k]==b[k-1]时可以剪枝也就是后移枚举位置 #include<iostream> #include<cstdio> #include& ...

  6. 5.replace的用法,while循环,continue

    1.relace s='dadk12' print(s.replace('生活','dd'))==打印返回dadk12,即使没有也不会报错. 2.while continue while循环输出 1 ...

  7. iOS音频与视频的开发(二)- 使用AVAudioRecorder进行录制音频

    1.使用AVAudioRecorder录制视频 AVAudioRecorder与AVAudioPlayer类似,它们都属于AVFoundation的类.AVAudioRecorder的功能类似于一个录 ...

  8. profile和bashrc

    转自某不知名网友 /etc/profile,/etc/bashrc 是系统全局环境变量设定~/.profile,~/.bashrc用户家目录下的私有环境变量设定当登入系统时候获得一个shell进程时, ...

  9. DFS HDOJ 5348 Ponds

    题目传送门 题意:有一张无向图,度数小于2的点会被去掉,直到全都大于等于2,问连通块顶点数为奇数的权值和为多少 分析:首先DFS把度数小于2的vis掉,第二次DFS把属于同一个连通块的vis掉,检查是 ...

  10. 在Android 源码中添加系统服务

    Android系统本身提供了很多系统服务,如WindowManagerService,PowerManagerService等.下面描述一下添加一个系统服务的具体步骤. 1.定义自定义系统服务接口 撰 ...