Minimum Operations to Reduce X to Zero LeetCode solution in JavaScript

1
var minOperations = function (nums, x) {
2
let len = nums.length,
3
best = 0;
4
for (let i = 1; i < len; i++) nums[i] += nums[i - 1];
5
let y = nums[len - 1] - x;
6
if (y < 0) return -1;
7
if (y === 0) return len;
8
for (let i = -1, j = (l = 0); i < len - best && l <= x; l = nums[++i]) {
9
while (nums[j] - l < y) j++;
10
if (nums[j] - l === y) best = Math.max(best, j - i);
11
}
12
return best > 0 ? len - best : -1;
13
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0