Skip to content

Instantly share code, notes, and snippets.

@revanthpobala
Created March 25, 2017 05:39
Show Gist options
  • Save revanthpobala/8e1de890e09c12d9654471d16ef9b1f3 to your computer and use it in GitHub Desktop.
Save revanthpobala/8e1de890e09c12d9654471d16ef9b1f3 to your computer and use it in GitHub Desktop.
"""
Given a target, find the list of numbers that sum to the target
"""
def targetSum(a,k):
s={0:[]}
res = []
for x in a:
ns=dict(s)
for j in s:
if j+x == k:
res.append(s[j]+[x])
ns[j+x]=s[j]+[x]
s=ns
return res
print targetSum([-3,-5,9,12,0,3,2], 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment