VIKRAM91's blog

By VIKRAM91, history, 7 years ago, In English

I am doing this problem on Spoj and I have written the recursive solution. Which according to me is right. This solution is giving me TLE, so I change to memoization solution. This memoization solution is giving me WA.

Is my memoization of recursive solution wrong? If yes then why and if no then why is it not giving AC.

Is there any better way to do memoization?

Thank you.

Edit:-

I found my mistake. I was not initializing x,y,z. So they are taking garbage values for some testcases. Just initialize them with 0 and it's AC.

AC Solution

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +1 Vote: I do not like it

I think you forgot

if (dp[i] != null) return dp[i];\

although that might not fix your WA

»
7 years ago, # |
  Vote: I like it +4 Vote: I do not like it

My ac recursive solution .

Link