human
Minimize the cost to make all the adjacent elements distinct in an Array | Function that prints minimum cost required.
Dp - table.
Base case Not increasing the first element.
Increasing the first element by 1.
Increasing the first element by 2.
Condition if current element is not equal to previous non - increased element.
Condition if current element is not equal to previous element after being increased by 1.
Condition if current element is not equal to previous element after being increased by 2.
Take the minimum from all cases.
Finding the minimum cost.
Printing the minimum cost required to make all adjacent elements distinct.
Driver Code"
Solution in Python:
 ... Write me a code to minimize the cost to make all the adjacent elements distinct in an Array and the other steps required?
assistant
#Here is code to minimize the cost to make all the adjacent elements distinct in an Array and the other steps required.
 def minimumCost ( arr , cost , N ) :
	dp = [ [ 0 for i in range ( 3 ) ] for i in range ( N ) ]
	dp [ 0 ] [ 0 ] = 0
	dp [ 0 ] [ 1 ] = cost [ 0 ]
	dp [ 0 ] [ 2 ] = cost [ 0 ] * 2
	for i in range ( 1 , N ) :
		for j in range ( 3 ) :
			minimum = 1e6
			if ( j + arr [ i ] != arr [ i - 1 ] ) :
				minimum = min ( minimum , dp [ i - 1 ] [ 0 ] )
			if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) :
				minimum = min ( minimum , dp [ i - 1 ] [ 1 ] )
			if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) :
				minimum = min ( minimum , dp [ i - 1 ] [ 2 ] )
			dp [ i ] [ j ] = j * cost [ i ] + minimum
	ans = 1e6
	for i in range ( 3 ) :
		ans = min ( ans , dp [ N - 1 ] [ i ] )
	print ( ans )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 1 , 2 , 2 , 3 , 4 ]
	cost = [ 3 , 2 , 5 , 4 , 2 , 1 ]
	N = len ( arr )
	minimumCost ( arr , cost , N )