首页 > 其他 > 详细

[Leetcode]Largest Rectangle in Histogram

时间:2015-07-25 07:07:42      阅读:205      评论:0      收藏:0      [点我收藏+]

//用栈实现

class Solution {

public:
    int largestRectangleArea(vector<int>& height) {
    stack<int> index;
    vector<int> high=height;
    int result=0;
    int temp;
    high.push_back(0);
    for(int i=0;i<high.size();i++)
    {
        if(index.empty()||(!index.empty()&&high[i]>=high[index.top()]))
        {
            index.push(i);
            continue;
        }
        while(!index.empty()&&high[index.top()]>high[i])
        {
            int idx=index.top();
            index.pop();
            int width=(index.empty()?i:i-index.top()-1);//The most important line
            result=max(result,width*high[idx]);
        }
        index.push(i);
    }
    return result;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

[Leetcode]Largest Rectangle in Histogram

原文:http://blog.csdn.net/yang_snow/article/details/47052285

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!