human
Can you provide a code to maximize the diamonds by choosing different colour diamonds from adjacent boxes and the other steps required given the following:
Maximize the diamonds by choosing different colour diamonds from adjacent boxes | Function to return the maximized value.
Number of rows and columns.
Creating the Dp array.
Populating the first column.
Iterating over all the rows.
Getting the ( i - 1 ) th max value.
Adding it to the current cell.
Getting the max sum from the last column.
Driver code.
Columns are indexed 1 - based"
Solution in Python:
assistant
#Here is code to maximize the diamonds by choosing different colour diamonds from adjacent boxes and the other steps required.
 def maxSum ( arr ) :
	m = len ( arr )
	n = len ( arr [ 0 ] ) - 1
	dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m ) ]
	for i in range ( 1 , m , 1 ) :
		dp [ i ] [ 1 ] = arr [ i ] [ 1 ]
	for i in range ( 1 , n + 1 , 1 ) :
		for j in range ( 1 , m , 1 ) :
			mx = 0
			for k in range ( 1 , m , 1 ) :
				if ( k != j ) :
					if ( dp [ k ] [ i - 1 ] > mx ) :
						mx = dp [ k ] [ i - 1 ]
			if ( mx and arr [ j ] [ i ] ) :
				dp [ j ] [ i ] = arr [ j ] [ i ] + mx
	ans = - 1
	for i in range ( 1 , m , 1 ) :
		if ( dp [ i ] [ n ] ) :
			ans = max ( ans , dp [ i ] [ n ] )
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 10 , 2 , 20 , 0 ] , [ 0 , 0 , 0 , 5 , 0 ] , [ 0 , 0 , 0 , 0 , 6 ] , [ 0 , 4 , 0 , 11 , 5 ] , [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 ] ]
	print ( maxSum ( arr ) )