Leetcode 1658. Minimum Operations to Reduce X to Zero 前缀和数组题

  1. Minimum Operations to Reduce X to Zero
    Medium

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.

Example 1:

Input: nums = [1,1,4,2,3], x = 5
Output: 2
Explanation: The optimal solution is to remove the last two elements to reduce x to zero.
Example 2:

Input: nums = [5,6,7,8,9], x = 4
Output: -1
Example 3:

Input: nums = [3,2,20,1,1,3], x = 10
Output: 5
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.

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 104
1 <= x <= 109

解法1:用presums,backsums和unordered_map。时间复杂度应该是O(n)

class Solution {
    
    
public:
    int minOperations(vector<int>& nums, int x) {
    
    
        int n = nums.size();
        int res = n + 1;
        vector<int> presums(n + 1, 0), backsums(n + 1, 0);
        unordered_map<int, int> backMap;
        for (int i = 1; i <= n; i++) {
    
    
            presums[i] = presums[i - 1] + nums[i - 1];
        }
        for (int i = n; i >= 1; i--) {
    
    
            backsums[i - 1] = backsums[i] + nums[i - 1];
            backMap[backsums[i - 1]] = i - 1;
        }
        for (int i = 0; i <= n; i++) {
    
    
            if (presums[i] == x) {
    
    
                res = min(res, i);
            }
            if (backMap.find(x - presums[i]) != backMap.end()) {
    
    
                res = min(res, i + n - backMap[x - presums[i]]);
            }
        }
        return res == n + 1 ? -1 : res;
    }
};

解法2:基于presums,backsums和binary search。对每一个presums的entry,在backsums里面用binary search找x - presums[i]。时间复杂度应该是O(nlogn)。

class Solution {
    
    
public:
    int minOperations(vector<int>& nums, int x) {
    
    
        int n = nums.size();
        int res = n + 1;
        vector<int> presums(n + 1, 0), backsums(n + 1, 0);
        for (int i = 1; i <= n; i++) {
    
    
            presums[i] = presums[i - 1] + nums[i - 1];
        }
        for (int i = n; i >= 1; i--) {
    
    
            backsums[i - 1] = backsums[i] + nums[i - 1];
        }
        
        for (int i = 0; i <= n; i++) {
    
    
            int target = x - presums[i];
            int start = i, end = n;
            while (start + 1 < end) {
    
    
                int mid = start + (end - start) / 2;
                if (backsums[mid] > target) {
    
    
                    start = mid;
                } else if (backsums[mid] < target) {
    
    
                    end = mid;
                } else {
    
    
                    res = min(res, i + n - mid);    //(i + 1) + (n - mid)
                    break;
                }
            }
            if (backsums[start] == target) res = min(res, i + n - start);
            if (backsums[end] == target) res = min(res, i + n - end);
        }
        return res == n + 1 ? -1: res;
    }
};

猜你喜欢

转载自blog.csdn.net/roufoo/article/details/132788926