码迷,mamicode.com
首页 > 其他好文 > 详细

84. Largest Rectangle in Histogram (Array; Stack, DP)

时间:2015-10-08 21:27:26      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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 = [2,1,5,6,2,3].

 

技术分享

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.

法I: 使用stack记录高度。

class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        if(height.size() == 0) return 0;
         
        int res = 0;
        stack<int> idxStack;
        height.push_back(0); //为了如果在最后height为最高的情况,能够再进一次else,把stack中的元素pop出来计算
       
        for(int i = 0; i < height.size(); i++)
        {
            if(idxStack.empty() || (!idxStack.empty() && height[i] >= height[idxStack.top()])) //当前高度>=栈顶高度
                idxStack.push(i); //入栈
            else{ //高度降低了,那么再之后也就不可能超过height[idx],所以看之前的高度*宽度能够达到怎样的值
                while(!idxStack.empty() && height[idxStack.top()] > height[i]) //只要当前高度<栈顶高度
                {
                    int idx = idxStack.top(); 
                    idxStack.pop();
                    int width = idxStack.empty() ? i : (i-idxStack.top()-1); //当前index-1的位置(目前为止最高高度的位置)到当前栈顶元素的位置的宽度
                    res = max(res, height[idx] * width);
                }
                idxStack.push(i);
            }
        }
        height.pop_back();
        return res;
    }
};

法II: 使用动态规划

class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        int* l = new int [height.size()]; //状态:向左连续的>=height[i]的最后一个位置
        int* r = new int [height.size()]; //状态:向右连续的>=height[i]的最后一个位置
    
        l[0]=0;
        for( int i =1; i <height.size(); i++) //从左往右扫描
        {
            if(height[i]==0)
            {
                l[i] = 0;
                continue;
            }
            int j = i-1;
            for(; j >= 0; j--){
                if(height[i] <= height[j]) j = l[j]; //用动态规划求l[i]否则会Time Limit Exceeded
                if(height[i] > height[j]) break;
            }
            l[i] = j+1;
        }
        r[height.size()-1]=height.size()-1;
        for(int i =height.size()-2; i >=0; i--){ //从右往左扫描
            if(height[i]==0)
            {
                r[i] = 0;
                continue;
            }
            int j = i+1;
            for(; j <height.size(); j++){
                if(height[i] <= height[j]) j = r[j]; //动态规划
                if(height[i] > height[j]) break;
            }
            r[i] = j-1;
        }
        
        int area;
        int largestArea = 0;
        for(int i = 0; i< height.size(); i++)
        {
            area = (r[i]-l[i]+1) * height[i];
            if(area>largestArea) largestArea = area;
        }
        return largestArea;
    }
};

 

84. Largest Rectangle in Histogram (Array; Stack, DP)

标签:

原文地址:http://www.cnblogs.com/qionglouyuyu/p/4862175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!