|
| 1 | +# 1658. Minimum Operations to Reduce X to Zero |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Hash Table, Binary Search, Sliding Window, Prefix Sum. |
| 5 | +- Similar Questions: Minimum Size Subarray Sum, Subarray Sum Equals K, Minimum Operations to Convert Number, Removing Minimum Number of Magic Beans, Minimum Operations to Make the Integer Zero. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +Return **the **minimum number** of operations to reduce **`x` **to **exactly**** `0` **if it is possible****, otherwise, return **`-1`. |
| 12 | + |
| 13 | + |
| 14 | +Example 1: |
| 15 | + |
| 16 | +``` |
| 17 | +Input: nums = [1,1,4,2,3], x = 5 |
| 18 | +Output: 2 |
| 19 | +Explanation: The optimal solution is to remove the last two elements to reduce x to zero. |
| 20 | +``` |
| 21 | + |
| 22 | +Example 2: |
| 23 | + |
| 24 | +``` |
| 25 | +Input: nums = [5,6,7,8,9], x = 4 |
| 26 | +Output: -1 |
| 27 | +``` |
| 28 | + |
| 29 | +Example 3: |
| 30 | + |
| 31 | +``` |
| 32 | +Input: nums = [3,2,20,1,1,3], x = 10 |
| 33 | +Output: 5 |
| 34 | +Explanation: The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +- `1 <= nums.length <= 105` |
| 43 | + |
| 44 | +- `1 <= nums[i] <= 104` |
| 45 | + |
| 46 | +- `1 <= x <= 109` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +## Solution |
| 51 | + |
| 52 | +```javascript |
| 53 | +/** |
| 54 | + * @param {number[]} nums |
| 55 | + * @param {number} x |
| 56 | + * @return {number} |
| 57 | + */ |
| 58 | +var minOperations = function(nums, x) { |
| 59 | + var leftSumMap = { 0: 0 }; |
| 60 | + var rightSumMap = { 0: 0 }; |
| 61 | + var leftSum = 0; |
| 62 | + var rightSum = 0; |
| 63 | + var min = Number.MAX_SAFE_INTEGER; |
| 64 | + for (var i = 0; i < nums.length; i++) { |
| 65 | + leftSum += nums[i]; |
| 66 | + rightSum += nums[nums.length - 1 - i]; |
| 67 | + leftSumMap[leftSum] = i + 1; |
| 68 | + rightSumMap[rightSum] = i + 1; |
| 69 | + if (rightSumMap[x - leftSum] !== undefined && (i + 1 + rightSumMap[x - leftSum]) <= nums.length) { |
| 70 | + min = Math.min(min, i + 1 + rightSumMap[x - leftSum]); |
| 71 | + } |
| 72 | + if (leftSumMap[x - rightSum] !== undefined && (i + 1 + leftSumMap[x - rightSum]) <= nums.length) { |
| 73 | + min = Math.min(min, i + 1 + leftSumMap[x - rightSum]); |
| 74 | + } |
| 75 | + } |
| 76 | + return min === Number.MAX_SAFE_INTEGER ? -1 : min; |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +**Explain:** |
| 81 | + |
| 82 | +nope. |
| 83 | + |
| 84 | +**Complexity:** |
| 85 | + |
| 86 | +* Time complexity : O(n). |
| 87 | +* Space complexity : O(n * n). |
0 commit comments