📜  二维阵列中具有最大乘积的路径

📅  最后修改于: 2021-04-23 17:21:20             🧑  作者: Mango

给定大小为N * M的2D矩阵。任务是找到从(0,0)(N-1,M-1)的最大乘积路径。您只能从(i,j)移至(i,j + 1)并从(i,j)移至(i + 1,j)

例子

方法:
要从当前位置选择哪个方向,我们必须检查给出最大乘积的方向。我们将维护两个2D阵列:

  1. maxPath [i] [j]:它将存储最大乘积,直到(i,j)
  2. minPath [i] [j]:它将存储最小乘积,直到(i,j)

脚步:

  • 将maxPath [0] [0]和minPath [0] [0]初始化为arr [0] [0]。
  • 对于maxPath [i] [j]中所有剩余的像元,通过以下方法检查当前像元(i,j)与上一个像元(i-1,j)的乘积是否最大:
  • 对于minPath [i] [j]中所有剩余的像元,通过以下方法检查当前像元(i,j)与前一个像元(i-1,j)的乘积是否最小:
  • 更新minPath [i] [j] = min(minValue1,minValue2)maxPath [i] [j] = max(maxValue1,maxValue2)
  • 返回maxPath [n-1] [m-1]以获取最大乘积。

下面是上述方法的实现:

C++
// C++ Program to find maximum
// product path from (0, 0) to
// (N-1, M-1)
#include 
using namespace std;
#define N 3
#define M 3
  
// Function to find maximum product
int maxProductPath(int arr[N][M])
{
  
    // It will store the maximum
    // product till a given cell.
    vector > maxPath(N, vector(M, INT_MIN));
  
    // It will store the minimum
    // product till a given cell
    // (for -ve elements)
    vector > minPath(N, vector(M, INT_MAX));
  
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
  
            int minVal = INT_MAX;
            int maxVal = INT_MIN;
  
            // If we are at topmost
            // or leftmost, just
            // copy the elements.
            if (i == 0 && j == 0) {
                maxVal = arr[i][j];
                minVal = arr[i][j];
            }
  
            // If we're not at the
            // above, we can consider
            // the above value.
            if (i > 0) {
                int tempMax = max(maxPath[i - 1][j] * arr[i][j],
                                  minPath[i - 1][j] * arr[i][j]);
                maxVal = max(maxVal, tempMax);
  
                int tempMin = min(maxPath[i - 1][j] * arr[i][j],
                                  minPath[i - 1][j] * arr[i][j]);
                minVal = min(minVal, tempMin);
            }
  
            // If we're not on the
            // leftmost, we can consider
            // the left value.
            if (j > 0) {
                int tempMax = max(maxPath[i][j - 1] * arr[i][j],
                                  minPath[i][j - 1] * arr[i][j]);
                maxVal = max(maxVal, tempMax);
  
                int tempMin = min(maxPath[i][j - 1] * arr[i][j],
                                  minPath[i][j - 1] * arr[i][j]);
                minVal = min(minVal, tempMin);
            }
  
            // Store max & min product
            // till i, j.
            maxPath[i][j] = maxVal;
            minPath[i][j] = minVal;
        }
    }
  
    // Return the max product path
    // from 0, 0 -> N-1, M-1.
    return maxPath[N - 1][M - 1];
}
  
// Driver Code
int main()
{
    int arr[N][M] = { { 1, -2, 3 },
                      { 4, -5, 6 },
                      { -7, -8, 9 } };
  
    // Print maximum product from
    // (0, 0) to (N-1, M-1)
    cout << maxProductPath(arr) << endl;
    return 0;
}


Java
// Java Program to find maximum
// product path from (0, 0) to
// (N-1, M-1)
class GFG
{
  
static final int N = 3;
static final int M = 3;
  
// Function to find maximum product
static int maxProductPath(int arr[][])
{
  
    // It will store the maximum
    // product till a given cell.
    int [][]maxPath = new int[N][M];
      
    // It will store the minimum
    // product till a given cell
    // (for -ve elements)
    int [][]minPath = new int[N][M];
      
    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
  
            int minVal = Integer.MAX_VALUE;
            int maxVal = Integer.MIN_VALUE;
  
            // If we are at topmost
            // or leftmost, just
            // copy the elements.
            if (i == 0 && j == 0)
            {
                maxVal = arr[i][j];
                minVal = arr[i][j];
            }
  
            // If we're not at the
            // above, we can consider
            // the above value.
            if (i > 0) 
            {
                int tempMax = Math.max(maxPath[i - 1][j] * arr[i][j],
                                minPath[i - 1][j] * arr[i][j]);
                maxVal = Math.max(maxVal, tempMax);
  
                int tempMin = Math.min(maxPath[i - 1][j] * arr[i][j],
                                minPath[i - 1][j] * arr[i][j]);
                minVal = Math.min(minVal, tempMin);
            }
  
            // If we're not on the
            // leftmost, we can consider
            // the left value.
            if (j > 0) {
                int tempMax = Math.max(maxPath[i][j - 1] * arr[i][j],
                                minPath[i][j - 1] * arr[i][j]);
                maxVal = Math.max(maxVal, tempMax);
  
                int tempMin = Math.min(maxPath[i][j - 1] * arr[i][j],
                                minPath[i][j - 1] * arr[i][j]);
                minVal = Math.min(minVal, tempMin);
            }
  
            // Store max & min product
            // till i, j.
            maxPath[i][j] = maxVal;
            minPath[i][j] = minVal;
        }
    }
  
    // Return the max product path
    // from 0, 0.N-1, M-1.
    return maxPath[N - 1][M - 1];
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[][] = { { 1, -2, 3 },
                    { 4, -5, 6 },
                    { -7, -8, 9 } };
  
    // Print maximum product from
    // (0, 0) to (N-1, M-1)
    System.out.print(maxProductPath(arr) +"\n");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python Program to find maximum
# product path from (0, 0) to
# (N-1, M-1)
import sys
N = 3;
M = 3;
  
# Function to find maximum product
def maxProductPath(arr):
  
    # It will store the maximum
    # product till a given cell.
    maxPath = [[0 for i in range(M)] for j in range(N)];
  
    # It will store the minimum
    # product till a given cell
    # (for -ve elements)
    minPath = [[0 for i in range(M)] for j in range(N)];
  
    for i in range(N):
        for j in range(M):
  
            minVal = sys.maxsize;
            maxVal = -sys.maxsize;
  
            # If we are at topmost
            # or leftmost, just
            # copy the elements.
            if (i == 0 and j == 0):
                maxVal = arr[i][j];
                minVal = arr[i][j];
              
            # If we're not at the
            # above, we can consider
            # the above value.
            if (i > 0):
                tempMax = max(maxPath[i - 1][j] * arr[i][j],\
                            minPath[i - 1][j] * arr[i][j]);
                maxVal = max(maxVal, tempMax);
  
                tempMin = min(maxPath[i - 1][j] * arr[i][j],\
                    minPath[i - 1][j] * arr[i][j]);
                minVal = min(minVal, tempMin);
              
            # If we're not on the
            # leftmost, we can consider
            # the left value.
            if (j > 0):
                tempMax = max(maxPath[i][j - 1] * arr[i][j],\
                    minPath[i][j - 1] * arr[i][j]);
                maxVal = max(maxVal, tempMax);
  
                tempMin = min(maxPath[i][j - 1] * arr[i][j],\
                    minPath[i][j - 1] * arr[i][j]);
                minVal = min(minVal, tempMin);
              
            # Store max & min product
            # till i, j.
            maxPath[i][j] = maxVal;
            minPath[i][j] = minVal;
          
    # Return the max product path
    # from 0, 0.N-1, M-1.
    return maxPath[N - 1][M - 1];
  
# Driver Code
if __name__ == '__main__':
    arr = [[ 1, -2, 3 ],[ 4, -5, 6 ],[ -7, -8, 9]];
  
    # Prmaximum product from
    # (0, 0) to (N-1, M-1)
    print(maxProductPath(arr));
  
# This code is contributed by 29AjayKumar


C#
// C# Program to find maximum
// product path from (0, 0) to
// (N-1, M-1)
using System;
  
class GFG
{
   
static readonly int N = 3;
static readonly int M = 3;
   
// Function to find maximum product
static int maxProductPath(int [,]arr)
{
   
    // It will store the maximum
    // product till a given cell.
    int [,]maxPath = new int[N, M];
       
    // It will store the minimum
    // product till a given cell
    // (for -ve elements)
    int [,]minPath = new int[N, M];
       
    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
   
            int minVal = int.MaxValue;
            int maxVal = int.MinValue;
   
            // If we are at topmost
            // or leftmost, just
            // copy the elements.
            if (i == 0 && j == 0)
            {
                maxVal = arr[i, j];
                minVal = arr[i, j];
            }
   
            // If we're not at the
            // above, we can consider
            // the above value.
            if (i > 0) 
            {
                int tempMax = Math.Max(maxPath[i - 1,j] * arr[i,j],
                                minPath[i - 1,j] * arr[i,j]);
                maxVal = Math.Max(maxVal, tempMax);
   
                int tempMin = Math.Min(maxPath[i - 1,j] * arr[i,j],
                                minPath[i - 1,j] * arr[i,j]);
                minVal = Math.Min(minVal, tempMin);
            }
   
            // If we're not on the
            // leftmost, we can consider
            // the left value.
            if (j > 0) {
                int tempMax = Math.Max(maxPath[i,j - 1] * arr[i,j],
                                minPath[i,j - 1] * arr[i,j]);
                maxVal = Math.Max(maxVal, tempMax);
   
                int tempMin = Math.Min(maxPath[i,j - 1] * arr[i,j],
                                minPath[i,j - 1] * arr[i,j]);
                minVal = Math.Min(minVal, tempMin);
            }
   
            // Store max & min product
            // till i, j.
            maxPath[i, j] = maxVal;
            minPath[i, j] = minVal;
        }
    }
   
    // Return the max product path
    // from 0, 0.N - 1, M - 1.
    return maxPath[N - 1, M - 1];
}
   
// Driver Code
public static void Main(String[] args)
{
    int [,]arr = { { 1, -2, 3 },
                    { 4, -5, 6 },
                    { -7, -8, 9 } };
   
    // Print maximum product from
    // (0, 0) to (N - 1, M - 1)
    Console.Write(maxProductPath(arr) +"\n");
}
}
  
// This code is contributed by 29AjayKumar


输出:
2016

时间复杂度: O(N * M)
辅助空间: O(N * M)