largest rectangle in histogram leetcode
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的更多相关文章
- Largest Rectangle in Histogram leetcode java
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 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 ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
随机推荐
- 【转】构建Maven项目自动下载jar包
原文地址:https://blog.csdn.net/gfd54gd5f46/article/details/54973954 使用Maven 自动下载jar包 右键单击项目,将项目 转换成Maven ...
- Android Layout XML属性研究--android:layout_marginBottom (转载)
转自:http://blog.csdn.net/yanfangjin/article/details/7393023 在如下的xml配置文件中,起初对于android:layout_marginBot ...
- API网关——Kong实践分享
概述 01 什么是Kong Kong是一个在Nginx中运行的Lua应用程序,可以通过lua-nginx模块实现,Kong不是用这个模块编译Nginx,而是与OpenRestry一起发布,OpenRe ...
- ionic4+angular7+cordova上传图片
安装插件 安装插件Image Picker $ ionic cordova plugin add cordova-plugin-telerik-imagepicker $ npm install @i ...
- poj3417 闇の連鎖 【树上差分】By cellur925
闇の連鎖(yam.pas/c/cpp)题目描述传说中的暗之连锁被人们称为 Dark.Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它.经过研究,你发现 Dark 呈现无向图的结构,图中有 ...
- iSCSI 原理和基础使用
终于完成最后一篇了,一上午的时间就过去了. 下文主要是对基本操作和我对iSCSI的理解,网上有很多iSCSI原理,在这里我就不写了,请自行学习. 这篇文章仅对iSCSI的很多误解做一次梳理,你必须对所 ...
- css width
转载:http://blog.csdn.net/dddddz/article/details/8631655
- Xor-sequences CodeForces - 691E || 矩阵快速幂
Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...
- 当css样式表遇到层2
9.定制层的display属性:层的表现是通过框这种结构来实现的.框可以是块级对象也可以是行内对象. Display属性就是用来控制其中内容是块级还是行级.定义为block则为kuai块级,inlin ...
- 145 Binary Tree Postorder Traversal 二叉树的后序遍历
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...