📌  相关文章
📜  删除数组末尾元素以最大化乘积之和

📅  最后修改于: 2021-09-22 10:22:04             🧑  作者: Mango

给定一个由N 个正整数组成的数组。我们可以从两端中的任何一个移除元素,即从数组的左侧或右侧。每次我们删除一个元素时,分数都会增加元素 * 的值(已经删除的元素数 + 1)。任务是找到通过删除所有元素可以获得的最大分数。
例子:

Input : arr[] = { 1, 3, 1, 5, 2 }.
Output : 43
Remove 1 from left side (score = 1*1 = 1)
then remove 2, score = 1 + 2*2 = 5
then remove 3, score = 5 + 3*3 = 14
then remove 1, score = 14 + 1*4 = 18
then remove 5, score = 18 + 5*5 = 43.

Input :  arr[] = { 1, 2 }
Output : 5.

这个想法是使用动态规划。制作一个名为 dp[][] 的二维矩阵,初始化为 0,其中 dp[i][j] 表示从数组的索引 i 到索引 j 的索引得分的最大值。因此,我们的最终结果将存储在 dp[0][n-1] 中。
现在,dp[i][j] 的值将是 arr[i] *(已删除的元素数 + 1)+ dp[i+ 1][j] 或 arr[j] *(已删除的元素数)的最大值+ 1) + dp[i][j – 1]。
下面是这个方法的实现:

C++
// CPP program to find maximum score we can get
// by removing elements from either end.
#include 
#define MAX 50
using namespace std;
 
int solve(int dp[][MAX], int a[], int low, int high,
                                          int turn)
{
    // If only one element left.
    if (low == high)
        return a[low] * turn;
 
    // If already calculated, return the value.
    if (dp[low][high] != 0)
        return dp[low][high];
 
    // Computing Maximum value when element at
    // index i and index j is to be choosed.
    dp[low][high] = max(a[low] * turn + solve(dp, a,
                            low + 1, high, turn + 1),
                        a[high] * turn + solve(dp, a,
                           low, high - 1, turn + 1));
 
    return dp[low][high];
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 3, 1, 5, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int dp[MAX][MAX];
    memset(dp, 0, sizeof(dp));
 
    cout << solve(dp, arr, 0, n - 1, 1) << endl;
    return 0;
}


Java
// Java program to find maximum score we can get
// by removing elements from either end.
 
public class GFG {
 
    static final int MAX = 50;
 
    static int solve(int dp[][], int a[], int low, int high,
            int turn) {
        // If only one element left.
        if (low == high) {
            return a[low] * turn;
        }
 
        // If already calculated, return the value.
        if (dp[low][high] != 0) {
            return dp[low][high];
        }
 
        // Computing Maximum value when element at
        // index i and index j is to be choosed.
        dp[low][high] = Math.max(a[low] * turn + solve(dp, a,
                low + 1, high, turn + 1),
                a[high] * turn + solve(dp, a,
                        low, high - 1, turn + 1));
 
        return dp[low][high];
    }
 
// Driven Program
    public static void main(String args[]) {
        int arr[] = {1, 3, 1, 5, 2};
        int n = arr.length;
 
        int dp[][] = new int[MAX][MAX];
 
        System.out.println(solve(dp, arr, 0, n - 1, 1));
 
    }
}
 
/*This code is contributed by 29AjayKumar*/


Python 3
# Python 3 program to find maximum
# score we can get by removing
# elements from either end.
MAX = 50
 
def solve(dp, a, low, high, turn):
 
    # If only one element left.
    if (low == high):
        return a[low] * turn
 
    # If already calculated,
    # return the value.
    if (dp[low][high] != 0):
        return dp[low][high]
 
    # Computing Maximum value when element 
    # at index i and index j is to be choosed.
    dp[low][high] = max(a[low] * turn + solve(dp, a,
                          low + 1, high, turn + 1),
                        a[high] * turn + solve(dp, a,
                          low, high - 1, turn + 1));
 
    return dp[low][high]
 
# Driver Code
if __name__ == "__main__":
    arr = [ 1, 3, 1, 5, 2 ]
    n = len(arr)
 
    dp = [[0 for x in range(MAX)]
             for y in range(MAX)]
 
    print(solve(dp, arr, 0, n - 1, 1))
 
# This code is contributed by ChitraNayal


C#
// C# program to find maximum score we can get
// by removing elements from either end.
using System;
 
class GFG
{
    static int MAX = 50;
     
    static int solve(int[,] dp, int[] a, int low,
                            int high, int turn)
    {
        // If only one element left.
        if (low == high)
            return a[low] * turn;
     
        // If already calculated, return the value.
        if (dp[low, high] != 0)
            return dp[low, high];
     
        // Computing Maximum value when element at
        // index i and index j is to be choosed.
        dp[low,high] = Math.Max(a[low] * turn + solve(dp, a,
                                low + 1, high, turn + 1),
                            a[high] * turn + solve(dp, a,
                            low, high - 1, turn + 1));
     
        return dp[low, high];
    }
     
    // Driven code
    static void Main()
    {
        int[] arr = new int[]{ 1, 3, 1, 5, 2 };
        int n = arr.Length;
     
        int[,] dp = new int[MAX,MAX];
        for(int i = 0; i < MAX; i++)
            for(int j = 0; j < MAX; j++)
                dp[i, j] = 0;
     
        Console.Write(solve(dp, arr, 0, n - 1, 1));
    }
}
 
// This code is contributed by DrRoot_


PHP


Javascript


输出:

43

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程