Minimum steps to minimize n as per given condition | function to calculate min steps ; base case ; store temp value for n as min ( f ( n - 1 ) , f ( n2 ) , f ( n3 ) ) + 1 ; store memo [ n ] and return ; This function mainly initializes memo [ ] and calls getMinSteps ( n , memo ) ; initialize memoized array ; driver program
def getMinSteps ( n , memo ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( memo [ n ] != - 1 ) : NEW_LINE INDENT return memo [ n ] NEW_LINE DEDENT res = getMinSteps ( n - 1 , memo ) NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT res = min ( res , getMinSteps ( n // 2 , memo ) ) NEW_LINE DEDENT if ( n % 3 == 0 ) : NEW_LINE INDENT res = min ( res , getMinSteps ( n // 3 , memo ) ) NEW_LINE DEDENT memo [ n ] = 1 + res NEW_LINE return memo [ n ] NEW_LINE DEDENT def getsMinSteps ( n ) : NEW_LINE INDENT memo = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT memo [ i ] = - 1 NEW_LINE DEDENT return getMinSteps ( n , memo ) NEW_LINE DEDENT n = 10 NEW_LINE print ( getsMinSteps ( n ) ) NEW_LINE