📌  相关文章
📜  最大化通过值 i 跳转索引选择的元素 arr[i] 的总和

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

最大化通过值 i 跳转索引选择的元素 arr[i] 的总和

给定一个数组arr[] ,任务是计算数组中元素的最大总和,这样,如果选择了第 i 个元素,则将arr[i]添加到总和中,并且从当前索引开始向前跳转.

例子

方法:该任务可以使用动态规划来解决。取一个dp容器,该容器将存储从该特定索引到数组末尾的最大分数,并在找出所有索引的最大分数后打印它们的最大分数。
请按照以下步骤解决问题:

  • 创建一个与数组和ans变量大小相同的Dp容器。
  • 现在将dp[n-1]分配为arr[n-1]因为该索引的最大总和是a[n-1]
  • 从 n-2 到 0 向后迭代循环。
  • 对于每个索引,将 arr[i] 添加到 dp 并检查
    • 如果 i+arr[i] 小于数组的大小
      • 如果是,将dp[i+arr[i]]添加到dp[i]。
  • 迭代 dp 数组并找到 dp 的最大值并将其存储在我们的 ans 变量中。
  • 最后,打印答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to to find the maximum
// score from the given array.
void findMaximumScore(int arr[], int n)
{
    // Initialize dp.
    int dp[n + 1] = { 0 };
    dp[n - 1] = arr[n - 1];
 
    // Store the max sum
    int ans = 0;
 
    // Iterating backwards from n-2 to 0.
    for (int i = n - 2; i >= 0; i--) {
        dp[i] = arr[i];
        int j = i + arr[i];
        if (j < n) {
            dp[i] += dp[j];
        }
    }
 
    // Finding the maximum
    // score present in the dp.
    for (int i = 0; i < n; i++) {
        ans = max(dp[i], ans);
    }
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    int n = 5;
    int arr[] = { 7, 3, 1, 2, 3 };
    findMaximumScore(arr, n);
    return 0;
}


Java
// Java program for the above approach
public class GFG {
     
    // Function to to find the maximum
    // score from the given array.
    static void findMaximumScore(int arr[], int n)
    {
       
        // Initialize dp.
        int dp[] = new int[n + 1];
        dp[n - 1] = arr[n - 1];
     
        // Store the max sum
        int ans = 0;
     
        // Iterating backwards from n-2 to 0.
        for (int i = n - 2; i >= 0; i--) {
            dp[i] = arr[i];
            int j = i + arr[i];
            if (j < n) {
                dp[i] += dp[j];
            }
        }
     
        // Finding the maximum
        // score present in the dp.
        for (int i = 0; i < n; i++) {
            ans = Math.max(dp[i], ans);
        }
        System.out.println(ans);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        int arr[] = { 7, 3, 1, 2, 3 };
        findMaximumScore(arr, n);
    }
 
}
 
// This code is contributed by AnkThon


Python3
# Python Program to implement
# the above approach
 
# Function to to find the maximum
# score from the given array.
def findMaximumScore(arr, n):
 
    # Initialize dp.
    dp = [0] * (n + 1)
    dp[n - 1] = arr[n - 1]
 
    # Store the max sum
    ans = 0
 
    # Iterating backwards from n-2 to 0.
    for i in range(n-2, -1, -1):
        dp[i] = arr[i]
        j = i + arr[i]
        if (j < n):
            dp[i] += dp[j]
         
    # Finding the maximum
    # score present in the dp.
    for i in range(n):
        ans = max(dp[i], ans)
 
    print(ans)
 
# Driver Code
n = 5
arr = [7, 3, 1, 2, 3]
findMaximumScore(arr, n)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
 
using System;
 
public class GFG {
     
    // Function to to find the maximum
    // score from the given array.
    static void findMaximumScore(int []arr, int n)
    {
     
        // Initialize dp.
        int []dp = new int[n + 1];
        dp[n - 1] = arr[n - 1];
     
        // Store the max sum
        int ans = 0;
     
        // Iterating backwards from n-2 to 0.
        for (int i = n - 2; i >= 0; i--) {
            dp[i] = arr[i];
            int j = i + arr[i];
            if (j < n) {
                dp[i] += dp[j];
            }
        }
     
        // Finding the maximum
        // score present in the dp.
        for (int i = 0; i < n; i++) {
            ans = Math.Max(dp[i], ans);
        }
        Console.WriteLine(ans);
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int n = 5;
        int []arr = { 7, 3, 1, 2, 3 };
        findMaximumScore(arr, n);
    }
 
}
 
// This code is contributed by AnkThon


Javascript


输出
7

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