其他分享
首页 > 其他分享> > 单调栈-Maximum Width Ramp

单调栈-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;
    }

  

标签:peek,int,res,Ramp,Maximum,Stack,Width,isEmpty,stack
来源: https://www.cnblogs.com/hyserendipity/p/12231151.html