直方图内最大矩形

题目
公司:美团
类型:单调栈
题意:找到一个数组中形成的最大矩形。leetcode84题原题。

class MaxInnerRec {
public:
    int countArea(vector<int> A, int n) {
        //单调栈
        stack<int> stk;
        stk.push(-1);
        int res = 0;
        for(int i = 0; i < n; i++){
            while(stk.top() != -1 && A[stk.top()] >= A[i]){
                int h = A[stk.top()];
                stk.pop();
                res = max(res, h *(i - stk.top()-1));
            }
            stk.push(i);
        }
        while(stk.top()!=-1){
             int h = A[stk.top()];
             stk.pop();
             res = max(res, h *(n - stk.top()-1));
        }
        return res;
    }
};
发布了1205 篇原创文章 · 获赞 54 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/SYaoJun/article/details/105152027