[LeetCode]Largest Rectangle in Histogram
2013-11-18 14:48
197 查看
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 =
思考:记录每个矩形最左最右比自己高的矩形下标,左右相减即为长,乘以矩形高,即为当前矩形可以组成矩形最大面积。求最大值。
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.
思考:记录每个矩形最左最右比自己高的矩形下标,左右相减即为长,乘以矩形高,即为当前矩形可以组成矩形最大面积。求最大值。
struct Node{ int height; int left; int right; int area; }; class Solution { public: int largestRectangleArea(vector<int> &height) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int i; int maxarea=0; int len=height.size(); Node *h=new Node[len+2]; for(i=1;i<=len;i++) { h[i].height=height[i-1]; h[i].left=i; h[i].right=i; } h[0].height=-1; h[len+1].height=-1; for(i=1;i<=len;i++) { while(h[i].height<=h[h[i].left-1].height) h[i].left=h[h[i].left-1].left; } for(i=len;i>=1;i--) { while(h[i].height<=h[h[i].right+1].height) h[i].right=h[h[i].right+1].right; } for(i=1;i<=len;i++) { h[i].area=h[i].height*(h[i].right-h[i].left+1); if(maxarea<h[i].area) maxarea=h[i].area; } delete []h; return maxarea; } };
相关文章推荐
- LeetCode —— Largest Rectangle in Histogram
- LeetCode: Largest Rectangle in Histogram
- leetcode Largest Rectangle in Histogram 单调栈
- [Leetcode] Largest Rectangle in Histogram
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- LeetCode Largest Rectangle in Histogram
- 解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)
- leetcode hard专杀之84. Largest Rectangle in Histogram
- [leetcode 84] Largest Rectangle in Histogram
- 【leetCode】Largest Rectangle in Histogram&Maximal Rectangle
- [leetcode] Largest Rectangle in Histogram
- [LeetCode] Largest Rectangle in Histogram 解题报告
- leetcode -- Largest Rectangle in Histogram -- 重点,多看几遍
- LeetCode 题目答案 Largest Rectangle in Histogram 极品解法的解释
- Leetcode | Largest Rectangle in Histogram
- LeetCode Largest Rectangle in Histogram又一个极品程序
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
- 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
- LeetCode-84-Largest Rectangle in Histogram 经典题目,单调栈
- leetcode笔记:Largest Rectangle in Histogram