Leetcode Coin Change doubt

Problem link :

My code:

Can you please tell me where my code is failing getting TLE even after using DP

hello @joshisanjay8467

u are using -1 for two things
a) for unvisited state
b) for states that are already visited but unsolvable.

and ur program is not able to distinguish between them thats why it always solving all those unsolvable states again.

to resolve it use INT_MAX for unsolvable states . and in the end (in coin change function)
if answer returned is INT_MAX then return -1 otherwise return the answer itself

check now=->

Got it !!! Thankyou Aman bhaiya !!!

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.

1 Like