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