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

[Leetcode]Largest Rectangle in Histogram

时间:2015-07-25 07:10:35      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:   leetcode   

//用栈实现

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

标签:   leetcode   

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

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