forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecrease Elements To Make Array Zigzag.java
38 lines (31 loc) · 1.17 KB
/
Decrease Elements To Make Array Zigzag.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
/*
firstly, check elements in odd indices are greater than its neighbours.
if not, decrease its neigbours and update the cost.
do same thing for even indices, because there can be two combinations as indicated in question.
*/
private int calculateCost(int[] nums, int start){
int res = 0;
int n = nums.length;
int[] arr = Arrays.copyOf(nums, nums.length); // nums array will be modified, so copy it.
for(int i=start;i<n;i+=2){
int prev = (i==0) ? Integer.MIN_VALUE : arr[i-1];
int cur = arr[i];
int next = (i == n-1) ? Integer.MIN_VALUE : arr[i+1];
if(prev < cur && next < cur)
continue;
if(prev >= cur){
res += prev-cur +1;
arr[i-1] = cur-1;
}
if(next >= cur){
res += next-cur +1;
arr[i+1] = cur-1;
}
}
return res;
}
public int movesToMakeZigzag(int[] nums) {
return Math.min(calculateCost(nums, 0), calculateCost(nums,1));
}
}