1658. Minimum Operations to Reduce X to Zero
1658. Minimum Operations to Reduce X to Zero
class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
n = len(nums)
total = sum(nums)
target = total - x
left = 0
right = 0
window = 0
l = float('-inf')
while right < n:
window += nums[right]
right += 1
while window > target and left < right:
window -= nums[left]
left += 1
if window == target:
l = max(l, right - left)
return -1 if l == float('-inf') else n - l