单调栈-Maximum Width Ramp

2020-01-23 19:39:26

问题描述:

单调栈-Maximum Width Ramp_第1张图片

问题求解:

    public int maxWidthRamp(int[] A) {
        Stack 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;
    }

  

你可能感兴趣的:(单调栈-Maximum Width Ramp)