cosmos icon indicating copy to clipboard operation
cosmos copied to clipboard

Dynamic Programming/CoinChange.cpp

Open saiprasanna94 opened this issue 9 years ago • 2 comments

For the following input, int coins[] = {1,2,3}; int amount = 4;

Expected Output : 4 .....({1,1,1,1},{1,1,2},{1,3},{2,2})

Actual Result : 7

Correct me if I am wrong.

saiprasanna94 avatar Oct 09 '17 15:10 saiprasanna94

Yes, the code needs fixing. The problem is that current solution is considering different permutations of the same subset as different solutions. This way it is returning 7 because it is counting all these solutions: {1, 1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {2, 1, 1}, {1, 3}, {3, 1}, {2, 2}, while {1, 2, 1}, {2, 1, 1} and {3, 1} shouldn't be counted.

Vitorvgc avatar Oct 10 '17 01:10 Vitorvgc

Please check PR #1116 - this fixes the above issue. Also added python implementation.

amartyaamp avatar Oct 10 '17 19:10 amartyaamp