[leetcode] 494. Target Sum

Question:

You are given a list of non-negative integers, a1, a2, …, an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3. 
Output: 5
Explanation: 

-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3

There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  • The length of the given array is positive and will not exceed 20.
  • The sum of elements in the given array will not exceed 1000.
  • Your output answer is guaranteed to be fitted in a 32-bit integer.

Solution:

这道题很有意思,想了很久想不出来,去看了评论区的答案,居然有人能想到如此巧妙的思路,如下:

原题是要从一个集合里,把所有的数进行加或减的运算得到目标数S,于是就将原题改为把原集合分成两个集合P和N,使得集合P的和减去另集合N的和,得到目标数S,求这样分成两个集合的分法。

于是就有了如下推导

                  sum(P) - sum(N) = target
sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)
                       2 * sum(P) = target + sum(nums)

这样,原问题就变为了,是否能在原集合中找到一个集合P,使得集合P的和等于目标数S加集合所有数的和的一半。这就转化为 subset sum 问题。

对于本题,动态规划的状态转移方程可表示为:

用 f(i, j) 表示用集合的前 i 个数 (x1,...,xi) 的和得到目标 j 的种数
初始化时:
f(0, 0) = 1, f(0, j) = 0 (j = 1,...,S)
对于i > 0:
f(i, j) = f(i-1, j) + f(i-1, j-xi)

可以看到,f(i, j) 的值只和 i-1 层的值有关,因此存储数组可以只用一维,而 f(i-1, j-xi) 需要用到 i-1 层的第 j 个数的前面的数,因此用一维数组存储的时候,遍历 j 的时候要从后遍历,保证 f(j-xi) 的值是 i-1 层的值。

此外,注意到 S + sum(nums) 必须是偶数,在一开始时也可以先判断。

这样就得到了时间复杂度为O(NS),空间复杂度为O(S)的动态规划方法。

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int S) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if (S > sum || S < -sum || (sum + S) & 1)
            return 0;
        S = (S + sum) >> 1;

        int dp[S + 1] = {0};
        dp[0] = 1;
        for (int i : nums)
            for (int j = S; j >= i; j--)
                dp[j] += dp[j-i];
        return dp[S];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值