Program to find maximum non negative product in a matrix in Python

Suppose we have a matrix of order m x n. Initially we are at the top-left corner cell (0, 0), and in each step, we can only move right or down in the matrix. Now among all possible paths from the top-left corner cell (0, 0) to bottom-right corner cell(m-1, n-1), we have to find the path with the maximum non-negative product. If the answer is too large, then return the maximum non-negative product modulo 10^9+7.

So, if the input is like

2 -4 2
2 -4 2
4 -8 2

then the output will be 256 because the path is the colored one,

2 -4 2
2 -4 2
4 -8 2

so product is [2 * 2 * (-4) * (-8) * 2] = 256.

To solve this, we will follow these steps −

  • p := 10^9+7
  • m := row count of matrix
  • n := column count of matrix
  • dp := a 2d matrix of which is of order with given matrix and fill with 0
  • for i in range 0 to m - 1, do
    • for j in range 0 to n - 1, do
      • if i is same as 0 and j is same as 0, then
        • dp[i, j] := make a pair (matrix[i, j], matrix[i, j])
      • otherwise when i is same as 0, then
        • ans1 := dp[i, j-1, 0] * matrix[i, j]
        • dp[i, j] := make a pair (ans1, ans1)
      • otherwise when j is same as 0, then
        • ans1 := dp[i-1, j, 0] * matrix[i, j]
        • dp[i, j] := make a pair (ans1, ans1)
      • otherwise,
        • ans1 := dp[i-1, j, 0] * matrix[i, j]
        • ans2 := dp[i-1, j, 1] * matrix[i, j]
        • ans3 := dp[i, j-1, 0] * matrix[i, j]
        • ans4 := dp[i, j-1, 1] * matrix[i, j]
        • maximum := maximum of ans1, ans2, ans3 and ans4
        • minimum := minimum of ans1, ans2, ans3 and ans4
        • if maximum
        • dp[i, j] := make a pair (minimum, minimum)
      • otherwise when minimum > 0, then
        • dp[i, j] := make a pair (maximum, maximum)
      • otherwise,
        • dp[i, j] := make a pair (maximum, minimum)
  • if dp[m-1, n-1, 0]
  • return -1
  • otherwise,
    • return dp[m-1, n-1, 0] % p
  • Example

    Let us see the following implementation to get better understanding −

    def solve(matrix):
       p = 1e9+7
       m = len(matrix)
       n = len(matrix[0])
    
       dp = [[0 for _ in range(n)] for _ in range(m)]
    
       for i in range(m):
          for j in range(n):
             if i == 0 and j == 0:
                dp[i][j] = [matrix[i][j], matrix[i][j]]
    
             elif i == 0:
                ans1 = dp[i][j-1][0] * matrix[i][j]
                dp[i][j] = [ans1, ans1]
    
             elif j == 0:
                ans1 = dp[i-1][j][0] * matrix[i][j]
                dp[i][j] = [ans1, ans1]
    
             else:
                ans1 = dp[i-1][j][0] * matrix[i][j]
                ans2 = dp[i-1][j][1] * matrix[i][j]
                ans3 = dp[i][j-1][0] * matrix[i][j]
                ans4 = dp[i][j-1][1] * matrix[i][j]
                maximum = max(ans1, ans2, ans3, ans4)
                minimum = min(ans1, ans2, ans3 ,ans4)
                if maximum  0 :
                   dp[i][j] = [maximum, maximum]
                else:
                   dp[i][j] = [maximum, minimum]
    
       if dp[m-1][n-1][0] 

    Input

    "pqpqrrr"

    Output

    256
    Updated on: 2021-10-04T11:53:09+05:30

    229 Views

    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements