Minimum cost to build N blocks from one block | Function to calculate min cost to build N blocks ; Initialize base case ; Recurence when i is odd ; Recurence when i is even ; Driver code
def minCost ( n , x , y , z ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ ( i + 1 ) // 2 ] + x + z , dp [ i - 1 ] + y ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = min ( dp [ i // 2 ] + x , dp [ i - 1 ] + y ) NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT n = 5 NEW_LINE x = 2 NEW_LINE y = 1 NEW_LINE z = 3 NEW_LINE print ( minCost ( n , x , y , z ) ) NEW_LINE