human
Give me a code to minimize cost of choosing and skipping array elements to reach end of the given array and the other steps required given the following:
Minimize cost of choosing and skipping array elements to reach end of the given array | Function to find the minimum cost to reach the end of the array from the first element.
Store the results.
Consider first index cost.
Find answer for each position i.
First Element.
Second Element.
For remaining element.
Consider min cost for skipping.
Last index represents the minimum total cost.
Driver Code.
Given X.
Given array cost [ ].
Function Call"
Solution in Python:
assistant
#Here is code to minimize cost of choosing and skipping array elements to reach end of the given array and the other steps required.
 def minimumCost ( cost , n , x ) :
	dp = [ 0 ] * ( n + 2 )
	dp [ 0 ] = cost [ 0 ]
	for i in range ( 1 , n ) :
		if ( i == 1 ) :
			dp [ i ] = cost [ i ] + dp [ i - 1 ]
		if ( i == 2 ) :
			dp [ i ] = cost [ i ] + min ( dp [ i - 1 ] , x + dp [ i - 2 ] )
		if ( i >= 3 ) :
			dp [ i ] = ( cost [ i ] + min ( dp [ i - 1 ] , min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) )
	print ( dp [ n - 1 ] )
if __name__ == ' _ _ main _ _ ' :
	X = 4
	cost = [ 6 , 3 , 9 , 2 , 1 , 3 ]
	N = len ( cost )
	minimumCost ( cost , N , X )