Target Sum
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:
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.
Analysis
Solution
DP - backpack 转化为背包问题后用DP求解
By (@yuxiangmusic),
The difference is that if you updatedp
while:
increasing
i
then the previous partial resultdp[i - coin]
is the result that has consideredcoin
alreadydecreasing
i
then the previous partial resultdp[i - coin]
is the result that has not consideredcoin
yet
Reference
Last updated