Minimum steps to reduce N to 0 by given operations | Function to find the minimum number to steps to reduce N to 0 ; Dictionary for storing the precomputed sum ; Bases Cases ; Check if n is not in dp then only call the function so as to reduce no of recursive calls ; Return the answer ; Given number N ; Function call
function count ( n ) { var dp = new Map ( ) ; dp . set ( 0 , 0 ) ; dp . set ( 1 , 1 ) ; if ( ! dp . has ( n ) ) dp . set ( n , 1 + Math . min ( n % 2 + count ( parseInt ( n / 2 ) ) , n % 3 + count ( parseInt ( n / 3 ) ) ) ) ; return dp . get ( n ) ; } var N = 6 ; document . write ( count ( N ) ) ;