最大化通过值 i 跳转索引选择的元素 arr[i] 的总和
给定一个数组arr[] ,任务是计算数组中元素的最大总和,这样,如果选择了第 i 个元素,则将arr[i]添加到总和中,并且从当前索引开始向前跳转.
例子:
Input: N = 5, arr[] = {7, 3, 1, 2, 3}
Output: 7
Explanation:
- Take the first element 7 to our score, then we move 7 index ahead and goes out of the array so the final answer is 7.
- Start from 2nd element 3 and add it to the score, then move 3 indices ahead and land on the last index of the array and add it to our score, so the final score will be 3+3=6.
- Start from 3rd element 1 and add to our score then move 1 index ahead and add 2 to our score, so the final score is =1+2=3.
- Start from 4th element 2 and add it to the score and move 2 indices ahead as we reach out of the array, the final score is 2.
- Start from 5th element 3 and add it to our score and our final score will be 3.
So from all the cases, the maximum score is 7.
Input: N = 2, arr[] = {3, 1}
Output: 3
方法:该任务可以使用动态规划来解决。取一个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]。
- 如果 i+arr[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)