Suppose we have an array of integers, the move operation actually selects any element and decreases it by 1. Array A is a zigzag array if 1 or 2 is executed &

- Each even-indexed element is greater than its neighbors, so A [0] & gt; A [1] & lt; A [2] & gt; A [3] & lt; A [4] & gt; & # 8230; and so on.
- Each odd-indexed element is greater than its neighbors, so A [0] & lt; A [1] & gt; A [2] & lt; A [3] & gt; A [4] & lt; & # 8230; and so on.

We have to find the minimum number of moves to convert the given array numbers to a zigzag array.

So if the array is like [1,2,3], then the output will be 2, since we can reduce 2 to 0 or from 3 to 1

- Define a method called solve (), it will take nums and run, this will work k is shown below:
- k: = 0
- for me ranges from start to length of numbers, increasing by 2
- left: = 100000 when i &1 & lt; 0, otherwise nums [i &1]
- right: = 100000 when i + 1 & gt; = length of numbers, otherwise nums [i + 1]
- temp: = (minimum left and right) &1 &nums [i]
- if temp & lt; 0, then k: = k + | temp |

- return to
- In the main method this will be
- ans: = solve (numbers, 0)
- ANS: = minimum ANS and solve (Nums, 1)
- return response

Let`s look at the following implementation to understand better &

class Solution (object): def solve (self, nums, start): k = 0 for i in range (start, len (nums), 2) : left = 100000 if i-1 & lt; 0 else nums [i-1] right = 10000 if i + 1 & gt; = len (nums) else nums [i + 1] temp = ( min (left, right) -1 - nums [i]) if temp & lt; 0: k + = abs (temp) return k def movesToMakeZigzag (self, nums): "" " : type nums: List [int] : rtype: int "" " ans = self.solve (nums, 0) ans = min (ans, self.solve (nums, 1)) return ans

[1,2,3]

2

X
# Submit new EBook