Coin Change 2 Leetcode

I wrote my code and stuck at a point
problem https://leetcode.com/problems/coin-change-2/

my solution code :


I know my solution is giving REDUNDANT answers like
2+2+1 and 1+2+2 , 2+1+2 are considered with permutations how to fix this any idea ?

hello @joshisanjay8467
u need to add one more stateiin ur recursion to avoid duplicate counting.

ur state should be like this->

solve(amount ,i ) -> ways to make amount using only [i…n-1] coins

solve(amount,i) = solve(amount,i+1) // we will never use i ,[i+1…n-1]
or
solve(amount -coin[i],i) ;// we have used one ith coin, and call it on remaining amount but again we can use [i…n-1]

1 Like

I solved it as u said
updated code: https://ide.codingblocks.com/s/473235
It is submitted but faster than 5% only tried number of times but still slow.
Can u suggest some optimization in code ?

sorry, it was because of the big do array creation I made it runtime dependant.
Thank you for helping !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.