494. Target Sum¶
Edited by Charles
Medium · Python · Dynamic Programming
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Knapsack Solution:
Recall 0/1 Knapsack Problem for the core idea. Left Group = the sum of numbers with + signs; Right Group = the sum of numbers with - signs.
Original study notes follow, including intermediate snippets and earlier attempts.
Knapsack Solution:
- Recall 0/1 Knapsack Problem for the core idea.
- Left Group = the sum of numbers with + signs; Right Group = the sum of numbers with - signs.
- Left + Right = Total (fixed) → Right = Total - Left
- Left - Right = Target (considering signs) → Left = (Total + Target) / 2. Based on the expression, we require
- Left is an integer.
- Total > abs(target). Say, Total = 5, Target = 10.
- dp[i] represents the number of ways to fully stuff the knapsack with size i.
- Initialization: dp[0] = 1, i.e. no way [].
- Iteration: since all items can be used for only once, the outer loop should be knapsack size, and the inner loop should be the item values in descending order to ensure only once usage.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | February 5, 2026 |
Python solution¶
class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
# addSum + minusSum = total
# minusSum = total - addSum
# addSum - minusSum = target
l = len(nums)
total = sum(nums)
if abs(target) > total:
return 0
if (total + target) % 2:
return 0
addSum = (total + target) // 2
dp = [0] * (addSum + 1)
# Initialization: the racksack has max value container 0;
# the combinations of positive numbers is 1,
# i.e. addSum = [] or no positive numbers.
dp[0] = 1
for i in range(l):
for j in range(addSum, nums[i] - 1, -1):
dp[j] += dp[j - nums[i]]
return dp[-1]