【java】849. Maximize Distance to Closest Person

问题原文点击打开链接

今天心情不好,不多说,上代码。

 public int maxDistToClosest(int[] seats) {
        Stack stack = new Stack<>();
        for (int i = 0;i1){
            int posFirst = stack.pop();
            int posSecond = stack.peek();
            //这样处理是为了处理末尾是0 的情况
            if (!ifTack){
                res = length-posFirst;
                ifTack = !ifTack;
            }
            int dist = posFirst - posSecond-1;
            res = Math.max(res,dist%2==1?dist/2+1:dist/2);
        }
        res = Math.max(res,stack.peek());
        return res;
    }

你可能感兴趣的:(leetcode)