首页 > 其他 > 详细

Decrease Elements To Make Array Zigzag

时间:2019-08-08 09:07:22      阅读:202      评论:0      收藏:0      [点我收藏+]

For this task, I did not complete it during the contest. This is taged by easy!

class Solution {
    public int movesToMakeZigzag(int[] nums) {
        int[] res = new int[2];
        int len = nums.length, left, right;
        for (int i = 0; i < len; i++) {
            left = i > 0 ? nums[i - 1] : 1001;
            right = i < len - 1 ? nums[i + 1] : 1001;
            res[ i % 2] += Math.max(0, nums[i] - Math.min(left, right) + 1);
        }
        return Math.min(res[0], res[1]);
    }
}

Answer is referenced by this [discussion] (https://leetcode.com/problems/decrease-elements-to-make-array-zigzag/discuss/350576/JavaC%2B%2BPython-Easy-and-concise)

...When the index is 0, left is out of boundary, so give this value a mock 1001, which is greater max in this nums

...When the index is length - 1, right is out of boundary, so give this value a mock 1001, which is greater max in this nums

Decrease Elements To Make Array Zigzag

原文:https://www.cnblogs.com/setnull/p/11318727.html

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