题目链接:https://leetcode.cn/problems/decrease-elements-to-make-array-zigzag/
# 解题思路
贪心 + 分类讨论
假设,a,b,c 是数组中三个连续下标的数,则考虑以下情况:
- 当 a,c 都小于 b 时,很明显,这种情况不需要进行减一操作
 - 当 a,c 不都小于 b( 
a>=b || c>=b)时,那么要想将数组变成锯齿数组所花费的操作数最少,只能对不满足条件的 a 或 c 进行减一操作 
综上所述,对于当前的数,每次只需要对与其相邻的两个数进行减一操作即可,因此可以分奇偶两种情况进行讨论,取操作数总和最小的。
# 提交结果

# 代码
class Solution {  | |
public int movesToMakeZigzag(int[] nums) {  | |
int n = nums.length;  | |
int[] arr = new int[n];  | |
int even = 0;  | |
int odd = 0;  | |
System.arraycopy(nums, 0, arr, 0 , n);  | |
        // 考虑偶数索引 | |
for (int i = 0; i < n; i += 2) {  | |
even += cal(nums, i);  | |
        } | |
        // 考虑奇数索引 | |
for (int i = 1; i < n; i += 2) {  | |
odd += cal(arr, i);  | |
        } | |
return Math.min(odd, even);  | |
    } | |
private int cal(int[] nums, int i) {  | |
int ret = 0;  | |
if (i > 0 && nums[i] <= nums[i - 1]) {  | |
ret += (nums[i - 1] - nums[i] + 1);  | |
        } | |
if (i < nums.length - 1 && nums[i] <= nums[i + 1]) {  | |
ret += (nums[i + 1] - nums[i] + 1);  | |
            // 减到比 nums [i] 小一 | |
nums[i + 1] = nums[i] - 1;  | |
        } | |
return ret;  | |
    } | |
} |