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