Largest Rectangle in Histogram-最大长方形
题目描述:
给定n个非负整数height[n],分别代表直方图条的高,每个条的宽设为1,求直方图中面积最大的矩形的面积
题目来源:
http://oj.leetcode.com/problems/largest-rectangle-in-histogram/
题目分析:
维护一个栈,保存直方图条的下标,当当前栈为空或者栈顶的下标所表示的元素不大于当前元素时,入栈,否则出栈,直到可以把当前元素压入栈中
(1)对于当前栈,假设序列为a1, a2,...ai, ai+1, a...栈顶,那么处于ai和ai+1之间的元素一定大于ai+1,如果他们中的最小元素小于等于ai+1,那么它一定在栈中,故栈中处于ai和ai+1之间的元素一定大于ai+1
(2)计算矩形的面积,可以考虑以待计算的元素为中心,向右扩展最远,并且向左扩展最远
(3)出栈时,计算以刚出栈的元素为高的最大矩形,它向左扩展最远到栈中的下一个元素,向右扩展最远到当前元素(因为当前元素比他小)

- 时间复杂度:O(n)
- 示例代码:
int maxArea(vector<int> vi) {
stack<int> st;
int maxArea = , i = ;
while(i <= n) {
if(st.empty() || vi[st.top()] <= vi[i]) {
st.push(i++);
} else {
int tmp = st.top();
st.pop();
maxArea = max(maxArea, vi[tmp] * (st.empty() ? i : i - st.top() - ));
}
}
return maxArea;
}
Largest Rectangle in Histogram-最大长方形的更多相关文章
- 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 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode: 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 ...
随机推荐
- HTML浅学入门---基础知识 (1)<基本规则>
HTML: 结构化文档,超文本标记语言 (一)四条基本规则 1.每个开始标记必须和结束标记配套使用.// <tag> </tag> 2.文档中必须包含唯一的打开和关闭标记 ...
- 点亮led【转载】
http://linux-sunxi.org/Cubieboard/Programming/StatusLEDs Accessing the status LEDs The Cubieboard ha ...
- Careercup - Google面试题 - 5724823657381888
2014-05-06 06:37 题目链接 原题: Given an array of (unsorted) integers, arrange them such that a < b > ...
- 【Integer To Roman】cpp
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 设置 Eclipse 智能代码提示,大幅度减少 alt+/ 使用频率,打每个字都出现代码提示的办法
以前网上有个英文版本的,现在自己汉化一下...O(∩_∩)O 哈哈 ~ http://www.cnblogs.com/lidabo/archive/2013/03/05/2944245.html ...
- 阿里云无线&前端团队是如何基于webpack实现前端工程化的
背景 前端经历了初期的野蛮生长(切图,写简单的特效)——为了兼容浏览器兼容性而出现的各种类库(JQUERY,YUI等——mv*(饱暖思淫欲,代码多了,也就想到怎样组织代码结构,backbone,ang ...
- bzoj 2821 分块处理
大题思路就是分块,将n个数分成sqrt(n)个块,然后 处理出一个w数组,w[i,j]代表第i个块到第j个块的答案 那么对于每组询问l,r如果l,r在同一个块中,直接暴力做就行了 如果不在同一个块中, ...
- BZOJ 3223 文艺平衡树 [codevs3303翻转区间]
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...
- swipejs的使用
<div id='slider' class='swipe'> <div class="swipe-wrap"> <div><img sr ...
- CentOS 有gcc没有g++
[root@localhost ~]# which gcc/usr/bin/gcc[root@localhost ~]# which g++/usr/bin/which: no g++ in (/us ...