human
Maximum non | Function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ].
Store dimension of grid.
Stores maximum product path.
Stores minimum product path.
Traverse the grid and update maxPath and minPath array.
Initialize to inf and - inf.
Base Case.
Calculate for row :.
Update the maximum.
Update the minimum.
Calculate for column :.
Update the maximum.
Update the minimum.
Update maxPath and minPath.
If negative product.
Otherwise.
Given matrix mat [ ] [ ].
Function Call"
Solution in Python:
assistant
#Here is code to maximum non and the other steps required.
 def maxProductPath ( grid ) :
	n , m = len ( grid ) , len ( grid [ 0 ] )
	maxPath = [ [ 0 for i in range ( m ) ] for j in range ( n ) ]
	minPath = [ [ 0 for i in range ( m ) ] for j in range ( n ) ]
	for i in range ( n ) :
		for j in range ( m ) :
			mn = float ( " inf " )
			mx = float ( " - inf " )
			if ( i == 0 and j == 0 ) :
				mx = grid [ i ] [ j ]
				mn = grid [ i ] [ j ]
			if i > 0 :
				tempmx = max ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) )
				mx = max ( mx , tempmx )
				tempmn = min ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) )
				mn = min ( mn , tempmn )
			if ( j > 0 ) :
				tempmx = max ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) )
				mx = max ( mx , tempmx )
				tempmn = min ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) )
				mn = min ( mn , tempmn )
			maxPath [ i ] [ j ] = mx
			minPath [ i ] [ j ] = mn
	if ( maxPath [ n - 1 ] [ m - 1 ] < 0 ) :
		return - 1
	else :
		return ( maxPath [ n - 1 ] [ m - 1 ] )
mat = [ [ 1 , - 2 , 1 ] , [ 1 , - 2 , 1 ] , [ 3 , - 4 , 1 ] ]
print ( maxProductPath ( mat ) )