📌  相关文章
📜  通过将每个元素与其索引相乘,可能的最大子序列总和

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

给定一个由N 个整数组成的数组arr[] ,任务是通过将结果子序列的每个元素乘以其索引(基于 1 的索引)来找到最大子序列总和。

例子:

朴素的方法:这个想法是使用递归从数组中生成所有可能的子序列,并计算每个子序列所需的总和并打印最大总和。

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

高效方法:上述方法可以使用动态规划进行优化,因为问题包含许多重叠的子问题。以下是步骤:

  • 初始化一个辅助矩阵dp[][] ,其中dp[i][j]存储长度为j的子序列的最大值,直到索引i
  • 遍历给定的数组,对于每个元素,有两种可能性:
    • 要么将当前元素包含在子序列总和中并增加子序列中的元素数。
    • 或者从子序列中排除当前元素并继续下一个元素。
  • 因此,递推关系由以下等式给出:
  • 通过对每个索引使用上述递推关系不断更新dp[][]表,并返回整个数组可能的最大和。
  • 在上述步骤后打印最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Initialize dp array
int dp[1005][1005];
 
// Function to find the maximum
// sum of the subsequence formed
int maximumSumUtil(int a[], int index,
                   int count, int n)
{
    // Base Case
    if (index > n || count > n + 1) {
        return 0;
    }
 
    // If already calculated
    // state occurs
    if (dp[index][count] != -1)
        return dp[index][count];
 
    // Include the current element
    int ans1 = maximumSumUtil(a, index + 1,
                              count + 1, n)
               + a[index] * count;
 
    // Exclude the current element
    int ans2 = maximumSumUtil(a, index + 1,
                              count, n);
 
    // Update the maximum ans
    return (dp[index][count]
            = max(ans1, ans2));
}
 
// Function to calculate maximum sum
// of the subsequence obtained
int maximumSum(int arr[], int N)
{
    // Initialise the dp array with -1
    memset(dp, -1, sizeof(dp));
 
    // Print the maximum sum possible
    cout << maximumSumUtil(arr, 0, 1,
                           N - 1);
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { -1, 2, -10, 4, -20 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    maximumSum(arr, N);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Initialize dp array
static int [][]dp = new int[1005][1005];
 
// Function to find the maximum
// sum of the subsequence formed
static int maximumSumUtil(int a[], int index,
                          int count, int n)
{
  // Base Case
  if (index > n || count > n + 1)
  {
    return 0;
  }
 
  // If already calculated
  // state occurs
  if (dp[index][count] != -1)
    return dp[index][count];
 
  // Include the current element
  int ans1 = maximumSumUtil(a, index + 1,
                            count + 1, n) +
                            a[index] * count;
 
  // Exclude the current element
  int ans2 = maximumSumUtil(a, index + 1,
                            count, n);
 
  // Update the maximum ans
  return (dp[index][count] =
          Math.max(ans1, ans2));
}
 
// Function to calculate maximum sum
// of the subsequence obtained
static void maximumSum(int arr[], int N)
{
  // Initialise the dp array with -1
  for(int i = 0; i < 1005; i++)
  {
    for (int j = 0; j < 1005; j++)
    {
      dp[i][j] = -1;
    }
  }
 
  // Print the maximum sum possible
  System.out.print(maximumSumUtil(arr, 0,
                                  1, N - 1));
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array
  int arr[] = {-1, 2, -10, 4, -20};
 
  // Size of the array
  int N = arr.length;
 
  // Function Call
  maximumSum(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Initialize dp array
dp = [[-1 for x in range(1005)]
          for y in range(1005)]
 
# Function to find the maximum
# sum of the subsequence formed
def maximumSumUtil(a, index, count, n):
 
    # Base Case
    if (index > n or count > n + 1):
        return 0
 
    # If already calculated
    # state occurs
    if (dp[index][count] != -1):
        return dp[index][count]
 
    # Include the current element
    ans1 = (maximumSumUtil(a, index + 1,
                              count + 1, n) +
                           a[index] * count)
 
    # Exclude the current element
    ans2 = maximumSumUtil(a, index + 1,
                          count, n)
 
    # Update the maximum ans
    dp[index][count] = max(ans1, ans2)
 
    return dp[index][count]
 
# Function to calculate maximum sum
# of the subsequence obtained
def maximumSum(arr, N):
 
    # Print the maximum sum possible
    print(maximumSumUtil(arr, 0, 1,
                             N - 1))
 
# Driver Code
 
# Given array
arr = [ -1, 2, -10, 4, -20 ]
 
# Size of the array
N = len(arr)
 
# Function call
maximumSum(arr, N)
 
# This code is contributed by Shivam Singh


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Initialize dp array
static int [,]dp = new int[1005, 1005];
 
// Function to find the maximum
// sum of the subsequence formed
static int maximumSumUtil(int []a, int index,
                          int count, int n)
{
  // Base Case
  if (index > n || count > n + 1)
  {
    return 0;
  }
 
  // If already calculated
  // state occurs
  if (dp[index, count] != -1)
    return dp[index, count];
 
  // Include the current element
  int ans1 = maximumSumUtil(a, index + 1,
                            count + 1, n) +
                            a[index] * count;
 
  // Exclude the current element
  int ans2 = maximumSumUtil(a, index + 1,
                            count, n);
 
  // Update the maximum ans
  return (dp[index, count] =
          Math.Max(ans1, ans2));
}
 
// Function to calculate maximum sum
// of the subsequence obtained
static void maximumSum(int []arr, int N)
{
  // Initialise the dp array with -1
  for(int i = 0; i < 1005; i++)
  {
    for (int j = 0; j < 1005; j++)
    {
      dp[i, j] = -1;
    }
  }
 
  // Print the maximum sum possible
  Console.Write(maximumSumUtil(arr, 0,
                               1, N - 1));
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array
  int []arr = {-1, 2, -10, 4, -20};
 
  // Size of the array
  int N = arr.Length;
 
  // Function Call
  maximumSum(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
15

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

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