Grokking Dynamic Programming Patterns for Coding Interviews
Vote

0% completed

​

Solution: Target Sum

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

class Solution: def findTargetSubsets(self, num, s): def dp(index, acc, memo): if (index, acc) in memo: return memo[(index, acc)] if index == len(num) and acc == s: return 1 if index == len(num) and acc != s: return 0 result = dp(index + 1, acc + num[index], memo) + dp(index + 1, acc - num[index], memo) memo[(index, acc)] = result return result return dp(0, 0, {})
A

Anonymous

· 4 years ago

Can we use the logic from "minimum difference between subsets" ? We can convert this problem to find the number of subsets which has the target as "sum(nums) - S".

Or am I missing something?

A

Anonymous

· 5 years ago

There seems to be a typo somewhere in the code. When I tried the same algorithm with Leetcode I get the wrong answer. https://leetcode.com/problems/target-sum/

Show 2 replies

Reading Progress

0%


Vote for new content