What is Memoization of given recursive solution of spoj problem DCEPC501 — Save Thy Toys

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.

Problem is that you have not initialised x , y and z in your recursive function and so it takes junk value and so is max(x,max(y,z)) may give wrong answer when x , y or z are not assigned anything .
Initialising them to zero will do the task .

1 Like

thanks. I almost spent a whole day on this question. Thanks a lot