📌  相关文章
📜  最大化相同大小子序列的相同索引元素的乘积

📅  最后修改于: 2021-09-22 09:47:57             🧑  作者: Mango

给定两个整数数组a[]b[] ,任务是从两个给定数组中找到两个相等长度子序列的相同索引元素的最大可能乘积。

例子:

方法:该问题类似于动态规划的最长公共子序列问题。下面解释了基于递归和记忆的自顶向下方法:

  • 让我们定义一个函数F(X, Y),它返回数组 a[0-X] 和 b[0-Y] 之间的最大标量积。
  • 以下是递归定义:
  • 使用二维数组进行记忆并避免重新计算相同的子问题。

下面是上述方法的实现:

C++
// C++ implementation to maximize
// product of same-indexed elements
// of same size subsequences
 
#include 
using namespace std;
 
#define INF 10000000
 
// Utility function to find the maximum
int maximum(int A, int B, int C, int D)
{
    return max(max(A, B), max(C, D));
}
 
// Utility function to find
// the maximum scalar product
// from the equal length sub-sequences
// taken from the two given array
int maxProductUtil(
    int X, int Y,
    int* A, int* B,
    vector >& dp)
{
    // Return a very small number
    // if index is invalid
    if (X < 0 or Y < 0)
        return -INF;
 
    // If the sub-problem is already
    // evaluated, then return it
    if (dp[X][Y] != -1)
        return dp[X][Y];
 
    // Take the maximum of all
    // the recursive cases
    dp[X][Y]
        = maximum(
            A[X] * B[Y]
                + maxProductUtil(
                      X - 1, Y - 1, A, B, dp),
            A[X] * B[Y],
            maxProductUtil(
                X - 1, Y, A, B, dp),
            maxProductUtil(
                X, Y - 1, A, B, dp));
 
    return dp[X][Y];
}
 
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
int maxProduct(int A[], int N,
               int B[], int M)
{
    // Initialize a 2-D array
    // for memoization
    vector >
    dp(N, vector(M, -1));
 
    return maxProductUtil(
        N - 1, M - 1,
        A, B, dp);
}
 
// Driver Code
int main()
{
    int a[] = { -2, 6, -2, -5 };
    int b[] = { -3, 4, -2, 8 };
 
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[0]);
 
    cout << maxProduct(a, n, b, m);
}


Java
// Java implementation to maximize
// product of same-indexed elements
// of same size subsequences
class GFG{
 
static final int INF = 10000000;
 
// Utility function to find the maximum
static int maximum(int A, int B,
                   int C, int D)
{
    return Math.max(Math.max(A, B),
                    Math.max(C, D));
}
 
// Utility function to find the
// maximum scalar product from
// the equal length sub-sequences
// taken from the two given array
static int maxProductUtil(int X, int Y,
                          int []A, int[] B,
                          int [][]dp)
{
     
    // Return a very small number
    // if index is invalid
    if (X < 0 || Y < 0)
        return -INF;
 
    // If the sub-problem is already
    // evaluated, then return it
    if (dp[X][Y] != -1)
        return dp[X][Y];
 
    // Take the maximum of all
    // the recursive cases
    dp[X][Y] = maximum(A[X] * B[Y] +
                       maxProductUtil(X - 1, Y - 1,
                                      A, B, dp),
                       A[X] * B[Y],
                       maxProductUtil(X - 1, Y,
                                      A, B, dp),
                       maxProductUtil(X, Y - 1,
                                      A, B, dp));
                                       
    return dp[X][Y];
}
 
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
static int maxProduct(int A[], int N,
                      int B[], int M)
{
     
    // Initialize a 2-D array
    // for memoization
    int [][]dp = new int[N][M];
    for(int i = 0; i < N; i++)
    {
       for(int j = 0; j < M; j++)
       {
          dp[i][j] = -1;
       }
    }
     
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp);
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = { -2, 6, -2, -5 };
    int b[] = { -3, 4, -2, 8 };
 
    int n = a.length;
    int m = b.length;
 
    System.out.print(maxProduct(a, n, b, m));
}
}
 
// This code is contributed by Amal Kumar Choubey


Python3
# Python3 implementation to maximize
# product of same-indexed elements
# of same size subsequences
INF = 10000000
 
# Utility function to find the maximum
def maximum(A, B, C, D):
     
    return max(max(A, B), max(C, D))
 
# Utility function to find
# the maximum scalar product
# from the equal length sub-sequences
# taken from the two given array
def maxProductUtil(X, Y, A, B, dp):
 
    # Return a very small number
    # if index is invalid
    if (X < 0 or Y < 0):
        return -INF
 
    # If the sub-problem is already
    # evaluated, then return it
    if (dp[X][Y] != -1):
        return dp[X][Y]
 
    # Take the maximum of all
    # the recursive cases
    dp[X][Y]= maximum(A[X] * B[Y] +
                      maxProductUtil(X - 1, Y - 1,
                                     A, B, dp),
                      A[X] * B[Y],
                      maxProductUtil(X - 1, Y, A,
                                     B, dp),
                      maxProductUtil(X, Y - 1, A,
                                     B, dp))
                           
    return dp[X][Y]
 
# Function to find maximum scalar
# product from same size sub-sequences
# taken from the two given array
def maxProduct(A, N, B, M):
 
    # Initialize a 2-D array
    # for memoization
    dp = [[-1 for i in range(m)]
              for i in range(n)]
 
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp)
 
# Driver Code
a = [ -2, 6, -2, -5 ]
b = [ -3, 4, -2, 8 ]
n = len(a)
m = len(b)
 
print(maxProduct(a, n, b, m))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# implementation to maximize
// product of same-indexed elements
// of same size subsequences
using System;
 
class GFG{
 
static readonly int INF = 10000000;
 
// Utility function to find the maximum
static int maximum(int A, int B,
                   int C, int D)
{
    return Math.Max(Math.Max(A, B),
                    Math.Max(C, D));
}
 
// Utility function to find the
// maximum scalar product from
// the equal length sub-sequences
// taken from the two given array
static int maxProductUtil(int X, int Y,
                          int []A, int[] B,
                          int [,]dp)
{
     
    // Return a very small number
    // if index is invalid
    if (X < 0 || Y < 0)
        return -INF;
 
    // If the sub-problem is already
    // evaluated, then return it
    if (dp[X, Y] != -1)
        return dp[X, Y];
 
    // Take the maximum of all
    // the recursive cases
    dp[X, Y] = maximum(A[X] * B[Y] +
                       maxProductUtil(X - 1, Y - 1,
                                        A, B, dp),
                       A[X] * B[Y],
                       maxProductUtil(X - 1, Y,
                                      A, B, dp),
                       maxProductUtil(X, Y - 1,
                                      A, B, dp));
                                         
    return dp[X, Y];
}
 
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
static int maxProduct(int []A, int N,
                      int []B, int M)
{
     
    // Initialize a 2-D array
    // for memoization
    int [,]dp = new int[N, M];
    for(int i = 0; i < N; i++)
    {
       for(int j = 0; j < M; j++)
       {
          dp[i, j] = -1;
       }
    }
     
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []a = { -2, 6, -2, -5 };
    int []b = { -3, 4, -2, 8 };
 
    int n = a.Length;
    int m = b.Length;
 
    Console.Write(maxProduct(a, n, b, m));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
54