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 &
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
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