I've tried this code but it does not work and results with 0. I've known the recursive one, but I'm trying to do it using for loop.
def minsteps(n):
memo =[0]*(n+1)
memo[0] = 0
memo[1] = 0
for i in range(2,n,1):
r = 1+memo[i-1]
if i%2 == 0:
r = min(r, 1+memo[i//2])
elif i%3 == 0:
r = min(r, 1+memo[i//3])
memo[i] = r
return memo[n]
This code is to provide a minimum steps requires for certain number to be 1 undergoing processes of minus 1, divide 2 and divide 3. For example: 6->2->1 [3] or 6->3->1 [3] or 6->5->4->2->4 [5]
Thus, the minimum steps is 3.