单调栈-Maximum Width Ramp

2020-01-23 19:39:26

问题描述:

问题求解:

    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;
    }

  

posted @ 2020-01-23 19:40  hyserendipity  阅读(111)  评论(0编辑  收藏  举报