Doubts for the output in the code

Sir, in the top-down approach the code gives output 3 for 5 but in the bottom-up approach, the code gives output 4.

Hey @pranjal123
U must have done something wrong otherwise both will give the same answer.

Here is the explanation for both the cases
5->4->3->2->1
5->4->3->1
5->4->2->1
This are 3 possible ways of top down approach so answer is 4(min length path)

In Bottom Up
dp[1]=1
dp[2]=1+min(dp[2-1],dp[2/2])=1+min(dp[1],dp[1])=2
dp[3]=1+min(dp[3-1],dp[3/3])=1+min(dp[2],dp[1])=2
dp[4]=1+min(dp[4-1],dp[4/2])=1+min(dp[3],dp[2])=3
dp[5]=1+dp[5-1]=1+dp[4]=4
So this also gives 4

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.