Skip to content

Commit 3297ff4

Browse files
committed
Time: 214 ms (48.34%) | Memory: 37.8 MB (33.90%) - LeetSync
1 parent 0d40a4e commit 3297ff4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

494-target-sum/target-sum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
3+
dp = {}
4+
def helper(ind, sum):
5+
if ind == len(nums):
6+
return 1 if sum == target else 0
7+
if (ind, sum) in dp:
8+
return dp[(ind, sum)]
9+
10+
dp[(ind, sum)] = (helper(ind+1, sum + nums[ind]) +
11+
helper(ind+1, sum - nums[ind]))
12+
return dp[(ind, sum)]
13+
return helper(0, 0)
14+
15+
16+

0 commit comments

Comments
 (0)