给定两个整数数组a[]和b[] ,任务是从两个给定数组中找到两个相等长度子序列的相同索引元素的最大可能乘积。
例子:
Input: a[] = {-3, 1, -12, 7}, b[] = {3, 2, -6, 7}
Output: 124
Explanation: The subsequence [1, -12, 7] from a[] and subsequence [3, -6, 7] from b[] gives the maximum scaler product, (1*3 + (-12)*(-6) + 7*7) = 124.
Input: a[] = {-2, 6, -2, -5}, b[] = {-3, 4, -2, 8}
Output: 54
Explanation: The subsequence [-2, 6] from a[] and subsequence [-3, 8] from b[] gives the maximum scaler product, ((-2)*(-3) + 6*8) = 54.
方法:该问题类似于动态规划的最长公共子序列问题。下面解释了基于递归和记忆的自顶向下方法:
- 让我们定义一个函数F(X, Y),它返回数组 a[0-X] 和 b[0-Y] 之间的最大标量积。
- 以下是递归定义:
F(X, Y) = max(a[X] * b[Y] + F(X – 1, Y – 1); Use the last elements from both arrays and check further
a[X] * b[Y]; Only use the last element as the maximum product
F(X – 1, Y); Ignore the last element from the first array
F(X, Y – 1)); Ignore the last element from the second element
- 使用二维数组进行记忆并避免重新计算相同的子问题。
下面是上述方法的实现:
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
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live