Initially I solved it without using any DP, I got TLE. But, I finally solved it without getting TLE by using Bottom-Up DP approach. But, when I approached the problem using Top-Down DP , I got âwrong answerâ(NOT TLE). Although Top Down DP was also giving right answers when I manually entered large sample inputs. But still the result was âWrong answerâ when submitted in this IDE. I would have left this thing there and forgot had I got âTLEâ, but I donât understand why my Top-Down is giving âwrong answerâ. P.S: I have solved it without any issue by using Bottom-Up DP, but just wanted to know whatâs wrong with my Top-Down DP and why its giving âWrong Answerâ(not TLE). Here is my code(Includes both the approaches I used)- https://ide.codingblocks.com/s/227719
Tilling problem-2 related doubt
@a_krisna22 There are two changes that you need to do in your dp code, 1st one is if(n<=0)return 0
because if you do dp[n]=0 then the index can be negative.
Also initialize your dp your with 0 for every test case, because your dp array is global but there will be cases for other test cases when ânâ is same as previous but with different âmâ.
Rest all is correct.
Feel free to ask if any doubt remains.
Otherwise please mark this doubt as resolved if you got it.