Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 645 Bytes

1144.md

File metadata and controls

23 lines (21 loc) · 645 Bytes

1144. Decrease Elements To Make Array Zigzag

Solution 1 (time O(n), space O(1))

class Solution(object):
    def movesToMakeZigzag(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        def solve(pos):
            ans = 0
            for i in range(pos, len(nums), 2):
                t = 0
                if i - 1 >= 0:
                    t = max(t, nums[i] - nums[i - 1] + 1)
                if i + 1 <len(nums):
                    t = max(t, nums[i] - nums[i + 1] + 1)
                ans += t
            return ans
        return min(solve(0), solve(1))