最大直方图面积

栈中存高度单调递增的下标

int LargestRectangleArea(int a[],int n)
{
    a[n++]=0;
    stack<int> s;
    int LargestArea = 0;
    int temp, i = 0;
    while (i < n)
    {
        if (s.empty() || (a[i] > a[s.top()]))
        {
            s.push(i);
            i++;
        }
        else
        {
            temp = s.top();
            s.pop();
            LargestArea = max(LargestArea, a[temp] *(s.empty() ? i : i - s.top() - 1) );
        }
    }
    return LargestArea;
}
posted @ 2020-05-11 21:13  Zeronera  阅读(136)  评论(0)    收藏  举报