单调栈-Maximum Width Ramp

2020-01-23 19:39:26

问题描述:

单调栈-Maximum Width Ramp

问题求解:

    public int maxWidthRamp(int[] A) {
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        int n = A.length;
        for (int i = 0; i < n; i++) {
            if (stack.isEmpty() || A[stack.peek()] > A[i]) {
                stack.add(i);
            }
        }
        for (int i = n - 1; i > res; i--) {
            while (!stack.isEmpty() && A[stack.peek()] <= A[i]) {
                res = Math.max(res, i - stack.pop());
            }
        }   
        return res;
    }

  

上一篇:后端码农谈前端(CSS篇)第八课:继承与层叠


下一篇:SQL中存储过程和函数的区别