cosmos
cosmos copied to clipboard
Dynamic Programming/CoinChange.cpp
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.
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.
Please check PR #1116 - this fixes the above issue. Also added python implementation.