Doubt in Digit Dp

I am doing the standard digit dp to get sum of digits. But getting wrong answer. Can you check my technique?

hello @kks2029

dont paas and memoise count , that might take too much memory

instead try this way->
solve(idx,tight) = current digit + solve(idx+1,updated tight)

base case if idx==n return 0

I dont think dats the problem because the constraints arent that big. Also I m not getting run time or memory limit I m getting wrong ans

Also if I remove the count part It will increase the time complexity

1000000000 999999999
consider this test case Its giving faulty ans here 82 should be the ans! I cant figure it out
@aman212yadav

no, that will work fine infact thata space efficient.

removeif in line 19 and then try

if I remove if in line 19 then I ll have to call memset twice Should I do that?

yead u need to call memset twice.

Thannks bro my code got accepted I included memset twice and removed the extra check for tight


But I followed this article for the problem where they have given another way to escape using memset twice. Because calling memset everytime kind of looks waste to me . So could you point out the mistake here?

U can’t use result of one probleme in other problem.
Here for different value of n ,dp states will have different answers.
I prefer to solve independently to avoid any issue

so is this article wrong? or this is some different scenario altogethher.? I cant understand
I get your point but if I dont use tight values in my dp table the other vlaues for any value will remain unrestricted and hence should behave in same way so we may use it? so makes sense?

Yeah.try submitting their code and see it’s working or not

Its working It seems I m doing some silly mistake!!


This is the new code its working fine

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.