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;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值