Minimize operations required to obtain N | ''Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initial state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minimum operations to obtain n ; Driver code
import NEW_LINE def minOperations ( n ) : NEW_LINE INDENT dp = [ sys . maxsize ] * ( n + 1 ) NEW_LINE dp [ 1 ] = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT x = dp [ i // 2 ] NEW_LINE if x + 1 < dp [ i ] : NEW_LINE INDENT dp [ i ] = x + 1 NEW_LINE DEDENT DEDENT if i % 3 == 0 : NEW_LINE INDENT x = dp [ i // 3 ] NEW_LINE if x + 1 < dp [ i ] : NEW_LINE INDENT dp [ i ] = x + 1 NEW_LINE DEDENT DEDENT x = dp [ i - 1 ] NEW_LINE if x + 1 < dp [ i ] : NEW_LINE INDENT dp [ i ] = x + 1 NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 15 NEW_LINE print ( minOperations ( n ) ) NEW_LINE