Input: Target sum
Output: count of all possible permutations with given sum from list [1,2,3]
Example input: 4 and corresponding output: 7
All possible pairs:
1, 1, 1, 1
1, 2, 1
1, 1, 2
1, 3
2, 1, 1
2, 2
3, 1
For these given permutations for explanation of output these all must be taken from [1,2,3]
for any input.
Here is what I've tried so far:
def combinationSum(self, candidates, target):
rests = [[] for _ in range(target)]
for num in candidates:
if num > target:
continue
else:
rests[target - num].append([num])
for cur_rest in range(target - 1, num - 1, -1):
for result in rests[cur_rest]:
rests[cur_rest - num].append(result + [num])
return rests[0]
s=Solution()
c=[1,2,3]
t=int(input())
print(s.combinationSum(c,t))