-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumOperationsToReduceXToZero
More file actions
41 lines (30 loc) · 1.13 KB
/
MinimumOperationsToReduceXToZero
File metadata and controls
41 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# You are given an integer array nums and an integer x.
# In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x.
# Note that this modifies the array for future operations.
# Return the minimum number of operations to reduce x to exactly 0 if it is possible, otherwise, return -1.
class Solution(object):
def minOperations(self, nums, x):
"""
:type nums: List[int]
:type x: int
:rtype: int
"""
target = sum(nums) - x
if target == 0:
return len(nums)
if target < 0:
return -1
numsLen = len(nums)
j = 0
res = -1
currentSum = 0
for i in range(numsLen):
currentSum += nums[i]
while currentSum > target:
currentSum -= nums[j]
j += 1
if currentSum == target:
res = max(i - j + 1, res)
if res == -1:
return -1
return numsLen - res