Consider the problem in which you have a value of N
and you need to calculate how many ways you can sum up to N
dollars using [1,2,5,10,20,50,100]
Dollar bills.
Consider the classic DP solution:
C = [1,2,5,10,20,50,100]
def comb(p):
if p==0:
return 1
c = 0
for x in C:
if x <= p:
c += comb(p-x)
return c
It does not take into effect the order of the summed parts. For example, comb(4)
will yield 5 results: [1,1,1,1],[2,1,1],[1,2,1],[1,1,2],[2,2]
whereas there are actually 3 results ([2,1,1],[1,2,1],[1,1,2]
are all the same).
What is the DP idiom for calculating this problem? (non-elegant solutions such as generating all possible solutions and removing duplicates are not welcome)