📌  相关文章
📜  通过在数组中执行给定操作获得的最大值

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

给定一个数组arr[] ,任务是找出最大的可获得值。允许用户将两个连续元素相加或相乘。但是,两次乘法运算之间必须至少有一次加法运算(即,不允许连续进行两次乘法运算)。
让数组元素为1, 2, 3, 4那么1 * 2 + 3 + 4是一个有效的运算,而1 + 2 * 3 * 4不是一个有效的运算,因为有连续的乘法运算。
例子:

Input : 5 -1 -5 -3 2 9 -4
Output : 33
Explanation:
The maximum value obtained by following the above conditions is 33. 
The sequence of operations are given as:
5 + (-1) + (-5) * (-3) + 2 * 9 + (-4) = 33

Input : 5 -3 -5 2 3 9 4
Output : 62

方法:
这个问题可以用动态规划解决。

  1. 假设维度为 n * 2 的二维数组 dp[][]。
  2. 如果最后一个操作是加法,dp[i][0] 表示到第 i 个位置的数组的最大值。
  3. dp[i][1] 表示如果最后一个操作是乘法,则表示到第 i 个位置的数组的最大值。

现在,由于不允许连续的乘法运算,递推关系可以认为是:

dp[i][0] = max(dp[ i - 1][0], dp[ i - 1][1]) + a[ i + 1];
dp[i][1] = dp[i - 1][0] - a[i] + a[i] * a[i + 1];

基本情况是:

dp[0][0] = a[0] + a[1];
dp[0][1] = a[0] * a[1];

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// A function to calculate the maximum value
void findMax(int a[], int n)
{
    int dp[n][2];
    memset(dp, 0, sizeof(dp));
     
    // basecases
    dp[0][0] = a[0] + a[1];
    dp[0][1] = a[0] * a[1];
    
    //Loop to iterate and add the max value in the dp array
    for (int i = 1; i <= n - 2; i++) {
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i + 1];
        dp[i][1] = dp[i - 1][0] - a[i] + a[i] * a[i + 1];
    }
 
    cout << max(dp[n - 2][0], dp[n - 2][1]);
}
 
// Driver Code
int main()
{
    int arr[] = { 5, -1, -5, -3, 2, 9, -4 };
    findMax(arr, 7);
}


Java
// Java implementation of the above approach
class GFG
{
     
    // A function to calculate the maximum value
    static void findMax(int []a, int n)
    {
        int dp[][] = new int[n][2];
        int i, j;
         
        for (i = 0; i < n ; i++)
            for(j = 0; j < 2; j++)
                dp[i][j] = 0;
 
        // basecases
        dp[0][0] = a[0] + a[1];
        dp[0][1] = a[0] * a[1];
         
        // Loop to iterate and add the
        // max value in the dp array
        for (i = 1; i <= n - 2; i++)
        {
            dp[i][0] = Math.max(dp[i - 1][0],
                                dp[i - 1][1]) + a[i + 1];
            dp[i][1] = dp[i - 1][0] - a[i] +
                        a[i] * a[i + 1];
        }
     
        System.out.println(Math.max(dp[n - 2][0],
                                    dp[n - 2][1]));
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { 5, -1, -5, -3, 2, 9, -4 };
        findMax(arr, 7);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the above approach
import numpy as np
 
# A function to calculate the maximum value
def findMax(a, n) :
 
    dp = np.zeros((n, 2));
     
    # basecases
    dp[0][0] = a[0] + a[1];
    dp[0][1] = a[0] * a[1];
     
    # Loop to iterate and add the max value in the dp array
    for i in range(1, n - 1) :
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i + 1];
        dp[i][1] = dp[i - 1][0] - a[i] + a[i] * a[i + 1];
 
    print(max(dp[n - 2][0], dp[n - 2][1]), end ="");
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 5, -1, -5, -3, 2, 9, -4 ];
    findMax(arr, 7);
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
 
class GFG
{
     
    // A function to calculate the maximum value
    static void findMax(int []a, int n)
    {
        int [,]dp = new int[n, 2];
        int i, j;
         
        for (i = 0; i < n ; i++)
            for(j = 0; j < 2; j++)
                dp[i, j] = 0;
 
        // basecases
        dp[0, 0] = a[0] + a[1];
        dp[0, 1] = a[0] * a[1];
         
        // Loop to iterate and add the
        // max value in the dp array
        for (i = 1; i <= n - 2; i++)
        {
            dp[i, 0] = Math.Max(dp[i - 1, 0], dp[i - 1, 1]) + a[i + 1];
                                 
            dp[i, 1] = dp[i - 1, 0] - a[i] + a[i] * a[i + 1];
        }
     
        Console.WriteLine(Math.Max(dp[n - 2, 0], dp[n - 2, 1]));
    }
     
    // Driver Code
    public static void Main()
    {
        int []arr = { 5, -1, -5, -3, 2, 9, -4 };
        findMax(arr, 7);
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
33

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