[LeetCode] Largest Rectangle in Histogram

来源:互联网 发布:淘宝介入后卖家不举证 编辑:程序博客网 时间:2024/06/07 00:17
[Problem]

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.

[LeetCode] Largest Rectangle in Histogram - coder007 - Coder007的博客

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

[LeetCode] Largest Rectangle in Histogram - coder007 - Coder007的博客

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given height = [2,1,5,6,2,3],
return 10.


[Analysis]

方法一:暴力
对于每一个height[i],从height[i]向两边找边界,复杂度为O(n^2),过Large Judge会TLE。

方法二:动态规划

(1)用left[i]记录从height[i]往左,连续的大于等于height[i]的左边界,按照从0~n更新left[i];
    对于j=height[i],则更新left[i]=left[j],否则停止对left[i]的更新,i++;
(2)用right[i]记录从height[i]往右,连续的大于等于height[i]的右边界,按照从n~0更新right[i];
    对于j>i,如果height[j]>=height[i],则更新right[i]=right[j],否则停止对right[i]的更新,i++

[Solution]

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int max = 0;
int len = height.size();
int left[len+1], right[len+1];

for(int i = 0; i < len; ++i){
left[i] = i;
while(left[i] >= 1 && height[i] <= height[left[i]-1]){
left[i] = left[left[i]-1];
}
}
for(int i = len - 1; i >= 0; --i){
right[i] = i;
while(right[i] <= len-2 && height[i] <= height[right[i]+1]){
right[i] = right[right[i]+1];
}
}
for(int i = 0; i < len; ++i){
// area
int area = (right[i] - left[i] + 1) * height[i];
max = max > area ? max : area;
}
return max;
}
};


 说明:版权所有,转载请注明出处。Coder007的博客
原创粉丝点击