Skip to content

Commit db139f3

Browse files
committed
Create 1658-minimum-operations-to-reduce-x-to-zero.java
1 parent 94bbd8b commit db139f3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int minOperations(int[] nums, int x) {
3+
int arrSum = IntStream.of(nums).sum();
4+
int target = arrSum - x, cur_sum = 0, max_win = -1, l = 0;
5+
6+
for (int r = 0; r < nums.length; r++) {
7+
cur_sum += nums[r];
8+
9+
while (l <= r && cur_sum > target) {
10+
cur_sum -= nums[l];
11+
l++;
12+
}
13+
14+
if (cur_sum == target)
15+
max_win = Math.max(max_win, r - l + 1);
16+
}
17+
18+
return max_win == -1 ? -1 : nums.length - max_win;
19+
}
20+
}

0 commit comments

Comments
 (0)