Skip to main content

1658 - Minimum Operations to Reduce X to Zero

Details

KeyValue
Linkhttps://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
LanguagePython 3
Runtime1199 ms, faster than 91.32% of Python3 online submissions for Minimum Operations to Reduce X to Zero
Memory Usage27.9 MB, less than 82.16% of Python3 online submissions for Minimum Operations to Reduce X to Zero
DatastructuresList[int]
AlgorithmsTwo Pointers

Procedure

  1. TBD...

Code

class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
sum_of_nums = sum(nums)
if sum_of_nums < x: return -1
if sum_of_nums == x: return len(nums)

required_subarray_sum = sum_of_nums - x
left = current_sum = max_subarray_size = 0
for right, num in enumerate(nums):
current_sum += num
while current_sum > required_subarray_sum:
current_sum -= nums[left]
left += 1
if current_sum == required_subarray_sum:
max_subarray_size = max(max_subarray_size, right-left+1)

return len(nums) - max_subarray_size if max_subarray_size > 0 else -1