Largest Rectangle in Histogram
2014-08-10 11:29
281 查看
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 =
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
维护一个不减栈,注意在height最后压入0,迫使栈的全部元素弹出。
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 =
10unit.
For example,
Given height =
[2,1,5,6,2,3],
return
10.
维护一个不减栈,注意在height最后压入0,迫使栈的全部元素弹出。
class Solution { public: int largestRectangleArea(vector<int> &height) { height.push_back(0); stack<int> stk; int i=0,maxArea=0; while(i<height.size()) { if(stk.empty()||height[i]>=height[stk.top()]) stk.push(i++); else{ int t = stk.top(); stk.pop(); maxArea = max(maxArea, height[t] * (stk.e mpty() ? i : i - stk.top()-1)); } } return maxArea; } };
相关文章推荐
- HDU 1506-Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram(单调栈)
- [LeetCode#84]Largest Rectangle in Histogram
- hdu 1506(nyoj 258)Largest Rectangle in a Histogram(最大长方形(二))(单调栈)
- HDU-1506 Largest Rectangle in a Histogram(单调栈)
- LeetCode84 --Largest Rectangle in Histogram
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
- HDU - 1506(单调队列) - Largest Rectangle in a Histogram
- Largest Rectangle in Histogram
- hdu 1506 Largest Rectangle in a Histogram
- **Leetcode_largest-rectangle-in-histogram(c++ and python version)
- 84. Largest Rectangle in Histogram
- hdu 1507 Largest Rectangle in a Histogram 动态规划计算最大面积
- Largest Rectangle in a Histogram
- (数组)Largest Rectangle in Histogram(栈解问题)
- [Leetcode] Largest Rectangle in Histogram
- [LeetCode] Largest Rectangle in Histogram、Maximal Rectangle
- hdu 1506 Largest Rectangle in a Histogram
- 杭电1506——Largest Rectangle in a Histogram
- LeetCode84 Largest Rectangle in Histogram