首页 > 其他 > 详细

单调栈-Maximum Width Ramp

时间:2020-01-23 20:07:22      阅读:101      评论:0      收藏:0      [点我收藏+]

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

  

单调栈-Maximum Width Ramp

原文:https://www.cnblogs.com/hyserendipity/p/12231151.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!