📌  相关文章
📜  根据给定条件最大化数组中子序列的总和

📅  最后修改于: 2021-09-17 07:37:34             🧑  作者: Mango

给定一个由N 个整数组成的数组a[] ,任务是执行以下操作:

  • 选择一个子序列,并为该子序列的每个第 p元素计算乘积p * a[i]
  • 计算p * a[i]的计算值的总和。
  • 应该选择子序列,使其最大化所需的总和。

例子:

朴素方法:解决问题的最简单方法是从数组中生成所有可能的子序列并计算每个子序列的总和。最后,求最大和。
时间复杂度: O(N 3 )
辅助空间: O(N)

高效方法:上述方法可以通过使用动态规划进行优化。请按照以下步骤解决问题:

  • 对于每个元素,存在两种可能性,即该元素要么是子序列的一部分,要么不是。
  • 初始化一个dp[][]矩阵,其中dp[i][j]存储最大值:
  • 通过考虑每个数组元素的上述条件,不断更新 dp[][] 表,并打印整个数组可能的最大和。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
const int N = 6;
  
// Function to select the array elements to
// maximize the sum of the selected elements
int maximumSum(int a[], int count, 
               int index, int n, 
               int dp[N][N])
{
    // If the entire array
    // is solved
    if (index == n)
        return 0;
  
    // Memoized subproblem
    if (dp[index][count] != -1)
        return dp[index][count];
  
    // Calculate sum considering the
    // current element in the subsequence
    int take_element = a[index] * count + 
                         maximumSum(a, count + 1, 
                                  index + 1, n, dp);
  
    // Calculate sum without considering the
    // current element in the subsequence
    int dont_take = maximumSum(a, count, 
                               index + 1, n, dp);
  
    // Update the maximum of the above sums
    // in the dp[][] table
    return dp[index][count] = max(take_element, 
                                  dont_take);
}
  
// Driver Code
int main()
{
    int n = 5;
    int a[] = { -1, -9, 0, 5, -7 };
  
    // Initialize the dp array
    int dp[N][N];
    memset(dp, -1, sizeof(dp));
  
    cout << (maximumSum(a, 1, 0, n, dp));
}
  
// This code is contributed by Rajput-Ji


Java
// Java program to implement
// the above approach
import java.util.*;
  
public class GFG {
  
    // Function to select the array elements to
    // maximize the sum of the selected elements
    public static int maximumSum(int[] a, int count,
                                 int index, int n,
                                 int[][] dp)
    {
        // If the entire array
        // is solved
        if (index == n)
            return 0;
  
        // Memoized subproblem
        if (dp[index][count] != -1)
            return dp[index][count];
  
        // Calculate sum considering the
        // current element in the subsequence
        int take_element
            = a[index] * count
              + maximumSum(a, count + 1,
                           index + 1, n, dp);
  
        // Calculate sum without considering the
        // current element in the subsequence
        int dont_take
            = maximumSum(a, count, index + 1, n, dp);
  
        // Update the maximum of the above sums
        // in the dp[][] table
        return dp[index][count]
            = Math.max(take_element, dont_take);
    }
  
    // Driver Code
    public static void main(String args[])
    {
        int n = 5;
        int a[] = { -1, -9, 0, 5, -7 };
  
        // Initialize the dp array
        int dp[][] = new int[n + 1][n + 1];
        for (int i[] : dp)
            Arrays.fill(i, -1);
  
        System.out.println(maximumSum(a, 1, 0, n, dp));
    }
}


Python3
# Python3 program to implement
# the above approach
  
# Function to select the array elements to
# maximize the sum of the selected elements
def maximumSum(a, count, index, n, dp):
  
    # If the entire array
    # is solved
    if(index == n):
        return 0
  
    # Memoized subproblem
    if(dp[index][count] != -1):
        return dp[index][count]
  
    # Calculate sum considering the
    # current element in the subsequence
    take_element = (a[index] * count + 
                 maximumSum(a, count + 1,
                               index + 1, 
                               n, dp))
  
    # Calculate sum without considering the
    # current element in the subsequence
    dont_take = maximumSum(a, count, 
                           index + 1, n, dp)
  
    # Update the maximum of the above sums
    # in the dp[][] table
    dp[index][count] = max(take_element,
                           dont_take)
                             
    return dp[index][count]
  
# Driver Code
n = 5
a = [ -1, -9, 0, 5, -7 ]
  
# Initialize the dp array
dp = [[-1 for x in range(n + 1)]
          for y in range(n + 1)]
  
# Function call
print(maximumSum(a, 1, 0, n, dp))
  
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
  
class GFG{
  
// Function to select the array elements to
// maximize the sum of the selected elements
public static int maximumSum(int[] a, int count,
                             int index, int n,
                             int[,] dp)
{
      
    // If the entire array
    // is solved
    if (index == n)
        return 0;
  
    // Memoized subproblem
    if (dp[index, count] != -1)
        return dp[index, count];
  
    // Calculate sum considering the
    // current element in the subsequence
    int take_element = a[index] * count + 
                       maximumSum(a, count + 1,
                                     index + 1,
                                     n, dp);
  
    // Calculate sum without considering the
    // current element in the subsequence
    int dont_take = maximumSum(a, count, 
                               index + 1, n, dp);
  
    // Update the maximum of the above sums
    // in the [,]dp table
    return dp[index, count] = Math.Max(take_element, 
                                       dont_take);
}
  
// Driver Code
public static void Main(String []args)
{
    int n = 5;
    int []a = { -1, -9, 0, 5, -7 };
  
    // Initialize the dp array
    int [,]dp = new int[n + 1, n + 1];
    for(int i = 0; i < n + 1; i++)
    {
        for(int j = 0; j < n + 1; j++)
        {
            dp[i, j] = -1;
        }
    }
    Console.WriteLine(maximumSum(a, 1, 0, n, dp));
}
}
  
// This code is contributed by PrinciRaj1992


Javascript


输出:
14

时间复杂度: O(N 2 )
辅助空间: O(N 2 )

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