You are given an integer array nums
and an integer target
.
You want to build an expression out of nums by adding one of the symbols ‘+’ and ‘-‘ before each integer in nums and then concatenate all the integers.
Return the number of different expressions that you can build, which evaluates to target.
Input: nums = [1,1,1,1,1], target = 3
Output: 5
Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
Input: nums = [1], target = 1
Output: 1
nums.length
is guaranteed to be at least 1.nums
contain zeros?
nums
can contain zeros.The problem is essentially to find the number of ways to assign +
and -
signs to the elements of the array so that their sum equals the target. This is a variation of the subset sum problem and can be approached using dynamic programming.
Here’s a concise step-by-step plan:
P
be the subset of elements that are given the +
sign.N
be the subset of elements that are given the -
sign.sum(P) - sum(N) = target
.Rearranging, we get:
sum(P) = (target + sum(nums)) / 2
This essentially transforms the problem into finding a subset with a sum of (target + sum(nums)) / 2
.
(target + sum(nums))
is odd, return 0 since it’s not possible to partition the sum into integers.dp[i]
represents the number of ways to achieve sum i
.dp[0]
to 1 since there is one way to achieve the sum 0: use no elements.nums
and update the dp
array.#include <vector>
#include <numeric> // for std::accumulate
class Solution {
public:
int findTargetSumWays(std::vector<int>& nums, int target) {
int sum = std::accumulate(nums.begin(), nums.end(), 0);
// If target + sum is not even, return 0 because (sumP) will not be an integer
if ((sum + target) % 2 != 0) return 0;
int subsetSum = (sum + target) / 2;
if(subsetSum < 0) return 0; // Negative subset sum is not feasible
std::vector<int> dp(subsetSum + 1, 0);
dp[0] = 1; // There's one way to get to sum 0: pick nothing
for (int num : nums) {
for (int j = subsetSum; j >= num; --j) {
dp[j] += dp[j - num];
}
}
return dp[subsetSum];
}
};
O(n * subsetSum)
, where n
is the number of elements in nums
, and subsetSum
is (sum + target) / 2
.O(subsetSum)
due to the dp array.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?