human
Write me a code to minimize total cost without repeating same task in two consecutive iterations and the other steps required given the following:
Minimize total cost without repeating same task in two consecutive iterations | Function to return the minimum cost for N iterations.
Construct the dp table.
1 st row of dp table will be equal to the 1 st of cost matrix.
Iterate through all the rows.
To iterate through the columns of current row.
Initialize val as infinity.
To iterate through the columns of previous row.
Fill the dp matrix.
Returning the minimum value.
Driver Code.
Number of iterations.
Number of tasks.
Cost matrix"
How can the above be solved in Python?
assistant
#Here is code to minimize total cost without repeating same task in two consecutive iterations and the other steps required.
 def findCost ( cost_mat , N , M ) :
	dp = [ [ 0 ] * M for _ in range ( M ) ]
	dp [ 0 ] = cost_mat [ 0 ]
	for row in range ( 1 , N ) :
		for curr_col in range ( M ) :
			val = 999999999
			for prev_col in range ( M ) :
				if curr_col != prev_col :
					val = min ( val , dp [ row - 1 ] [ prev_col ] )
			dp [ row ] [ curr_col ] = val + cost_mat [ row ] [ curr_col ]
	return min ( dp [ - 1 ] )
if __name__ == " _ _ main _ _ " :
	N = 4
	M = 4
	cost_mat = [ [ 4 , 5 , 3 , 2 ] , [ 6 , 2 , 8 , 1 ] , [ 6 , 2 , 2 , 1 ] , [ 0 , 5 , 5 , 1 ] ]
	print ( findCost ( cost_mat , N , M ) )