通过将给定数组的元素与给定乘数相乘来最大化分数
给定大小为N和M的两个数组array[]和multipliers[] ,其中N总是大于等于M。要执行M个操作。在每个操作中,从开头或结尾选择multiplier[i]和数组arr[]中的一个元素,假设为K ,然后将multiplier[i]*K添加到总分中,例如ans并从数组arr[中删除K ]。任务是找到最终得分ans的最大值。
例子:
Input: array[] = {1, 2, 3}, multipliers[] = {3, 2, 1}, N=3, M=3
Output: 14
Explanation: An optimal solution is as follows:
– Choose from the end, [1, 2, 3], adding 3 * 3 = 9 to the score.
– Choose from the end, [1, 2], adding 2 * 2 = 4 to the score.
– Choose from the end, [1], adding 1 * 1 = 1 to the score.
The total score is 9 + 4 + 1 = 14.
Input: array[] = {2, 1}, multipliers[] = {0}, N=2, M=1
Output: 0
Explanation: No matter 2 or 1 is chosen, the answer will be 0 because multiplier[0] equals 0.
天真的方法:蛮力解决方案是递归地检查每一对并找到最佳解决方案。
下面是上述方法的实现
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum score
// using dynamic programming and
// memoization
int getMaxScore(vector& array,
vector& multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.size(), N = array.size();
int remain = N - M;
vector dp(M + 1, 0);
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
int main()
{
vector array = { 1, 2, 3 };
vector multipliers = { 3, 2, 1 };
cout << getMaxScore(array, multipliers);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program for the above approach
public class GFG {
// Function to find the maximum score
// using dynamic programming and
// memoization
static int getMaxScore(int []array,int []multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.length;
int N = array.length;
int remain = N - M;
int dp[] = new int[M + 1];
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = Math.max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
public static void main (String[] args)
{
int []array = { 1, 2, 3 };
int []multipliers = { 3, 2, 1 };
System.out.println(getMaxScore(array, multipliers));
}
}
// This code is contributed by AnkThon
Python3
# Python program for the above approach
# Function to find the maximum score
# recursively
def getMaxScore(array, multipliers):
# Depth first search
def dfs(start, end, index):
if index == len(multipliers):
return 0
# Pick left
left = multipliers[index] * array[start] + \
dfs(start + 1, end, index + 1)
# Pick right
right = multipliers[index] * array[end] + \
dfs(start, end - 1, index + 1)
return max(right, left)
return dfs(0, len(array) - 1, 0)
# Driver Code
if __name__ == "__main__":
array = [1, 2, 3]
multipliers = [3, 2, 1]
print(getMaxScore(array, multipliers))
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the maximum score
// using dynamic programming and
// memoization
static int getMaxScore(int[] array, int[] multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.Length;
int N = array.Length;
int remain = N - M;
int[] dp = new int[M + 1];
for (int i = 0; i < M; ++i)
{
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j)
{
dp[j] = Math.Max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
public static void Main(String[] args)
{
int[] array = { 1, 2, 3 };
int[] multipliers = { 3, 2, 1 };
Console.Write(getMaxScore(array, multipliers));
}
}
// This code is contributed by gfgking.
Javascript
C++
// Java program for the above approach
#include
using namespace std;
// Function to find the maximum score
// using dynamic programming and
// memoization
int getMaxScore(vector& array,
vector& multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.size(), N = array.size();
int remain = N - M;
vector dp(M + 1, 0);
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
int main ()
{
vector array = { 1, 2, 3 };
vector multipliers = { 3, 2, 1 };
cout << getMaxScore(array, multipliers);
}
// This code is contributed by shikhasingrajput
Java
// Java program for the above approach
import java.util.*;
public class GFG {
// Function to find the maximum score
// using dynamic programming and
// memoization
static int getMaxScore(int []array,int []multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.length;
int N = array.length;
int remain = N - M;
int dp[] = new int[M + 1];
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = Math.max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
public static void main (String[] args)
{
int []array = { 1, 2, 3 };
int []multipliers = { 3, 2, 1 };
System.out.println(getMaxScore(array, multipliers));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find the maximum score
# using dynamic programming and
# memoization
def getMaxScore(array, multipliers):
# M is the number of elements needed to pick
M, N = len(multipliers), len(array)
remain = N - M
dp = [0] * (M + 1)
for i in range(M):
mm = multipliers[-i - 1]
for j in range(M - i):
dp[j] = max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j])
remain += 1
return dp[0]
# Driver Code
if __name__ == "__main__":
array = [1, 2, 3]
multipliers = [3, 2, 1]
print(getMaxScore(array, multipliers))
C#
// C# program for the above approach
using System;
public class GFG {
// Function to find the maximum score
// using dynamic programming and
// memoization
static int getMaxScore(int[] array, int[] multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.Length;
int N = array.Length;
int remain = N - M;
int[] dp = new int[M + 1];
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = Math.Max(mm * array[j] + dp[j + 1],
mm * array[j + remain]
+ dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
public static void Main(string[] args)
{
int[] array = { 1, 2, 3 };
int[] multipliers = { 3, 2, 1 };
Console.WriteLine(getMaxScore(array, multipliers));
}
}
// This code is contributed by ukasp.
Javascript
14
时间复杂度: O(2 M )
辅助空间: O(1)
高效方法:该解决方案基于动态规划,因为它包含两个属性——最优子结构和重叠子问题。假设dp[i][j]是可以从子数组中获得的当前最大结果,其中i是开始索引, j是结束。在任何阶段,都有两种选择:
pick the first: dp[i + 1][j] + curr_weight * array[i]
pick the last: dp[i][j – 1] + curr_weight * array[j]
结果将是两者的最大值。请按照以下步骤使用深度优先搜索和记忆来解决问题:
- 初始化一个变量保持为NM 。
- 用值0初始化大小为M+1的数组dp[] 。
- 使用变量i迭代范围[0, M)并执行以下步骤:
- 将变量mm初始化为multipliers[-i-1] 。
- 使用变量j迭代范围[0, Mi)并执行以下步骤:
- 将dp[j]的值设置为mm*array[j] + dp[j+1]或mm*array[j+remain] + dp[j] 的最大值。
- 将剩余的值增加1。
- 执行上述步骤后,打印dp[0]的值作为答案。
下面是上述方法的实现:
C++
// Java program for the above approach
#include
using namespace std;
// Function to find the maximum score
// using dynamic programming and
// memoization
int getMaxScore(vector& array,
vector& multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.size(), N = array.size();
int remain = N - M;
vector dp(M + 1, 0);
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
int main ()
{
vector array = { 1, 2, 3 };
vector multipliers = { 3, 2, 1 };
cout << getMaxScore(array, multipliers);
}
// This code is contributed by shikhasingrajput
Java
// Java program for the above approach
import java.util.*;
public class GFG {
// Function to find the maximum score
// using dynamic programming and
// memoization
static int getMaxScore(int []array,int []multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.length;
int N = array.length;
int remain = N - M;
int dp[] = new int[M + 1];
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = Math.max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
public static void main (String[] args)
{
int []array = { 1, 2, 3 };
int []multipliers = { 3, 2, 1 };
System.out.println(getMaxScore(array, multipliers));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find the maximum score
# using dynamic programming and
# memoization
def getMaxScore(array, multipliers):
# M is the number of elements needed to pick
M, N = len(multipliers), len(array)
remain = N - M
dp = [0] * (M + 1)
for i in range(M):
mm = multipliers[-i - 1]
for j in range(M - i):
dp[j] = max(mm * array[j] + dp[j + 1],
mm * array[j + remain] + dp[j])
remain += 1
return dp[0]
# Driver Code
if __name__ == "__main__":
array = [1, 2, 3]
multipliers = [3, 2, 1]
print(getMaxScore(array, multipliers))
C#
// C# program for the above approach
using System;
public class GFG {
// Function to find the maximum score
// using dynamic programming and
// memoization
static int getMaxScore(int[] array, int[] multipliers)
{
// M is the number of elements needed to pick
int M = multipliers.Length;
int N = array.Length;
int remain = N - M;
int[] dp = new int[M + 1];
for (int i = 0; i < M; ++i) {
int mm = multipliers[M - i - 1];
for (int j = 0; j < M - i; ++j) {
dp[j] = Math.Max(mm * array[j] + dp[j + 1],
mm * array[j + remain]
+ dp[j]);
}
remain += 1;
}
return dp[0];
}
// Driver Code
public static void Main(string[] args)
{
int[] array = { 1, 2, 3 };
int[] multipliers = { 3, 2, 1 };
Console.WriteLine(getMaxScore(array, multipliers));
}
}
// This code is contributed by ukasp.
Javascript
14
时间复杂度: O(M*M)
辅助空间: O(M)