Skip to content

Commit 8c5508b

Browse files
committed
Time: 19 ms (95.68%), Space: 17.7 MB (96.05%) - LeetHub
1 parent 2b555e7 commit 8c5508b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

0494-target-sum/0494-target-sum.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
3+
total = sum(nums)
4+
5+
if (target + total) % 2 != 0 or abs(target) > total:
6+
return 0
7+
8+
subset_sum = (target + total) // 2
9+
10+
dp = [0] * (subset_sum + 1)
11+
dp[0] = 1
12+
13+
for num in nums:
14+
for j in range(subset_sum, num - 1, -1):
15+
dp[j] += dp[j - num]
16+
17+
return dp[subset_sum]

0 commit comments

Comments
 (0)