3
firstly, check elements in odd indices are greater than its neighbours.4
if not, decrease its neigbours and update the cost.6
do same thing for even indices, because there can be two combinations as indicated in question.9
private int calculateCost(int[] nums, int start) {12
int[] arr = Arrays.copyOf(nums, nums.length); // nums array will be modified, so copy it.14
for (int i = start; i < n; i += 2) {15
int prev = (i == 0) ? Integer.MIN_VALUE : arr[i - 1];17
int next = (i == n - 1) ? Integer.MIN_VALUE : arr[i + 1];19
if (prev < cur && next < cur) continue;22
res += prev - cur + 1;27
res += next - cur + 1;34
public int movesToMakeZigzag(int[] nums) {35
return Math.min(calculateCost(nums, 0), calculateCost(nums, 1));