直方图最大矩形问题(Largest Rectangle in Histogram)
来源:互联网 发布:磁力链接网站源码 编辑:程序博客网 时间:2024/06/05 00:27
有n个非负整数,代表直方图中每个bar的高度,每个bar的宽度是1。请找出直方图中最大矩形的面积。
上面的直方图可以表示成一个数组{2,1,5,6,2,3}。
最大矩形面积是10.
思路:
要想找出面积最大的矩形的左边界和右边界,我们把矩形的左边界从数组的左边向右边移动,每移动一次,求一次最大矩形面积。最后再对所有矩形面积求最大值。这就是下面的算法的思路。
The point of this algorithm is to maintain a stack where higher element is always greater or equal to the lower element. Why do we need to maintain that kind of stack? Because if we have a non-decreasing list, we can easily calculate the maximum area in one scan. We just need to compare: height[i] * (n – i) for every i. So how do we maintain this stack? If we keep seeing larger element, we just need to push them onto the stack. If we see a smaller (compared to the top element on the stack) element, we need to do two things:
Pop the stack until we can maintain the non-decreasing order. Pushing the smaller element for m times, where m = number of poped elements.
Keep track of the maximum area that cause by those pop.
For example, we have height = {1,3,5,7,4}.
We push onto the stack for {1,3,5,7} then we see 4. 4 is less than 7, so we need to pop. We stop popping until we see 3. However many times we pop, we push 4 onto the stack. Therefore the resulted stack would be {1,3,4,4,4}. Because of popping 7, we need to remember that the maximum area that contains 7 is 7. The largest area that contains 5, the other element which get popped, is 10. So we take that down. We then finish processing all the elements in the original array and end up with a non-decreasing stack {1,3,4,4,4}. We can compute the largest area of this stack, which is 4*3 = 12. Since 12 is larger than the previous largest, 10, we output 12.
class Solution {public: int largestRectangleArea(vector<int> &height) { stack<int> stack_; int maxArea = 0; int i = 0; while(i < height.size()) { if(stack_.empty() || height[i] >= stack_.top()) { stack_.push(height[i]); i++; } else { int count = 0; while(!stack_.empty() && stack_.top() > height[i]) { count++; int top = stack_.top(); stack_.pop(); maxArea = max(maxArea, top * count); } for(int j = 0; j < count + 1; ++j) { stack_.push(height[i]); } i++; } } int count = 0; while(!stack_.empty()) { count++; maxArea = max(maxArea, stack_.top() * count); stack_.pop(); } return maxArea; }};
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- Largest Rectangle in Histogram 直方图中最大的矩形面积
- 寻找直方图中的最大矩形 Largest Rectangle in Histogram
- Largest Rectangle in Histogram 直方图中最大的矩形
- LeetCode 84. Largest Rectangle in Histogram(直方图最大矩形)
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- [LeetCode]—Largest Rectangle in Histogram 求直方图最大填充矩形面积
- LeetCode | Largest Rectangle in Histogram(直方图围城的最大矩形面积)
- LeetCode OJ 之 Largest Rectangle in Histogram (直方图中的最大矩形)
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- 84 Largest Rectangle in Histogram(直方图中最大的矩形)
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】
- 【直方图的最大面积】Largest Rectangle in Histogram
- Largest Rectangle in Histogram 直方图的最大正方形
- 4.1.3LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- ACM-直方图最大面积-Largest Rectangle in Histogram
- 字符串,整数的互转
- 北京主要书店今日门庭若市 莫言作品一书难求-莫言-诺贝尔文学奖-书店
- Android 4.1 - 将系统浏览器编译成独立应用
- 第一章:软件工程的概论
- 【NOSQL】非关系型数据库MongoDB ( MongoDB索引用法和效率分析 )
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- 开源项目之Android http请求及缓存框架(GalHttprequest)
- 第三章:软件计划
- Android UI组件
- MFC下拉框combo box控件实践笔记
- 开源项目之瀑布流DodoWaterFall
- 第四章:面向过程的软件设计方法
- javascript 中cookie的存储,获取cookie,删除cookie的方法。
- 【NOSQL】couchDB