human
Minimize cost to reach end of an array by two forward jumps or one backward jump in each move | Function to find the minimum cost to reach the end of an array.
Base Case : When N < 3.
Store the results in table.
Initialize base cases.
Iterate over the range [ 2 , N - 2 ] to construct the dp array.
Handle case for the last index , i . e . N - 1.
Print the answer.
Driver Code"
Solution in Python:
assistant
#Here is code to minimize cost to reach end of an array by two forward jumps or one backward jump in each move and the other steps required.
 def minCost ( arr , n ) :
	if ( n < 3 ) :
		print ( arr [ 0 ] )
		return
	dp = [ 0 ] * n
	dp [ 0 ] = arr [ 0 ]
	dp [ 1 ] = dp [ 0 ] + arr [ 1 ] + arr [ 2 ]
	for i in range ( 2 , n - 1 ) :
		dp [ i ] = min ( dp [ i - 2 ] + arr [ i ] , dp [ i - 1 ] + arr [ i ] + arr [ i + 1 ] )
	dp [ n - 1 ] = min ( dp [ n - 2 ] , dp [ n - 3 ] + arr [ n - 1 ] )
	print ( dp [ n - 1 ] )
if __name__ == " _ _ main _ _ " :
	arr = [ 9 , 4 , 6 , 8 , 5 ]
	N = len ( arr )
	minCost ( arr , N )