84.直方图中最大矩阵
2017-09-13 11:44
106 查看
Largest Rectangle in Histogram
问题描述:
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.![](https://leetcode.com/static/images/problemset/histogram_area.png)
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.
参考答案:
class Solution { public: int largestRectangleArea(vector<int>& heights) { stack<int> s; int i = 0; int maxarea = 0; while (i < heights.size()) { if (s.empty() || heights[i] >= heights[s.top()]) s.push(i++); else { int h = heights[s.top()]; s.pop(); int w = s.empty() ? i : i-s.top()-1; maxarea = max(maxarea, h*w); } } while (!s.empty()) { int h = heights[s.top()]; s.pop(); int w = s.empty() ? i : i-s.top()-1; maxarea = max(maxarea, h*w); } return maxarea; } };
性能:
相关文章推荐
- 单调栈——(直方图内最大矩形 || 最大全1子矩阵 )
- 直方图下的矩阵最大面积问题
- 算法题:直方图和0-1矩阵中最大矩形
- 直方图最大矩阵面积
- 直方图内最大矩阵
- 九章算法面试题43 直方图内最大矩阵
- 【矩阵二维或三维dp】最大子矩阵,子矩阵快速求和(用到最大直方图)
- 84 Largest Rectangle in Histogram(直方图中最大的矩形)
- 算法面试题——直方图内最大矩阵
- leetcode 85. Maximal Rectangle 最大子矩阵 + DP + 转换为最大直方图
- leetcode直方图的最大矩阵算法
- nyoj 104 最大和(子矩阵最大和)(枚举)
- UVA-10755 Garbage Heap 三维子矩阵最大和
- 35 求一个矩阵中最大的二维矩阵(元素和最大)
- Task 4.4二维环形数组求最大子矩阵之和
- 求最大矩阵的大小-------------->_<
- 最大流(矩阵解压 uva 11082)
- 动态规划——寻找子矩阵最大和
- 求01矩阵中的最大的正方形面积
- 矩阵局部最大值