2422. Merge Operations to Turn Array Into a Palindrome

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2422

class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        i, j = 0, len(nums) - 1
        a, b = nums[i], nums[j]
        res = 0
        while i < j:
            if a < b:
                i += 1
                a += nums[i]
                res += 1
            elif b < a:
                j -= 1
                b += nums[j]
                res += 1
            else:
                i, j = i + 1, j - 1
                a, b = nums[i], nums[j]
        return res