📌  相关文章
📜  通过将给定数组的元素与给定乘数相乘来最大化分数

📅  最后修改于: 2022-05-13 01:56:04.660000             🧑  作者: Mango

通过将给定数组的元素与给定乘数相乘来最大化分数

给定大小为NM的两个数组array[]multipliers[] ,其中N总是大于等于M。要执行M个操作。在每个操作中,从开头或结尾选择multiplier[i]和数组arr[]中的一个元素,假设为K ,然后将multiplier[i]*K添加到总分中,例如ans并从数组arr[中删除K ]。任务是找到最终得分ans的最大值。

例子:

天真的方法:蛮力解决方案是递归地检查每一对并找到最佳解决方案。

下面是上述方法的实现

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是结束。在任何阶段,都有两种选择:

结果将是两者的最大值。请按照以下步骤使用深度优先搜索和记忆来解决问题:

  • 初始化一个变量保持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)