📌  相关文章
📜  通过从任何第 i 个索引跳跃长度为 i + K * arr[i] 的数组来最大化总和

📅  最后修改于: 2021-09-06 05:49:09             🧑  作者: Mango

给定一个由N 个正整数和一个整数K组成的数组arr[] ,任务是通过从任何i节点(i + K*arr[i]) ( < N ) 长度来找到可能的数组元素的最大总和数组的索引。

例子:

朴素的方法:最简单的方法是在[0, N – 1]范围内为每个可能的起始索引遍历给定数组,并找到通过(i + K*arr[i]的跳转获得的所有和的最大值)来自每个可能的索引i 。在所有遍历之后打印获得的最大和。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:上述方法可以使用动态规划进行优化。这个想法是使用一个辅助数组dp[]使得dp[i]存储通过使用以下递推关系选择i作为起始索引可以获得的总和:

请按照以下步骤解决问题:

  • 用 { 0 } 初始化一个大小为N的辅助数组dp[]
  • 使用变量i在范围[N – 1, 0] 上迭代并执行以下步骤:
    • 如果(i + K*arr[i] ≥ N)的值则将dp[i]的值更新为arr[i]
    • 否则,将dp[i]的值更新为dp[i + K*arr[i]] + arr[i]
  • 完成上述步骤后,打印数组dp[]中的最大值作为结果的最大和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum
// possible by jumps of length
// i + K*arr[i] from any i-th index
void maxSum(int arr[], int N, int K)
{
    // Initialize an array dp[]
    int dp[N + 2] = { 0 };
 
    // Stores the maximum sum
    int maxval = 0;
 
    // Iterate over the range [N-1, 0]
    for (int i = N - 1; i >= 0; i--) {
 
        // If length of the jump exceeds N
        if ((i + K * arr[i]) >= N) {
 
            // Set dp[i] as arr[i]
            dp[i] = arr[i];
        }
 
        // Otherwise, update dp[i] as
        // sum of dp[i + K * arr[i]] and arr[i]
        else {
            dp[i] = dp[i + K * arr[i]] + arr[i];
        }
 
        // Update the overall maximum sum
        maxval = max(maxval, dp[i]);
    }
 
    // Print the answer
    cout << maxval;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 1, 3, 1, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
 
    maxSum(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the maximum sum
// possible by jumps of length
// i + K*arr[i] from any i-th index
static void maxSum(int arr[], int N, int K)
{
   
    // Initialize an array dp[]
    int[] dp = new int[N + 2];
    Arrays.fill(dp, 0);
 
    // Stores the maximum sum
    int maxval = 0;
 
    // Iterate over the range [N-1, 0]
    for (int i = N - 1; i >= 0; i--)
    {
 
        // If length of the jump exceeds N
        if ((i + K * arr[i]) >= N)
        {
 
            // Set dp[i] as arr[i]
            dp[i] = arr[i];
        }
 
        // Otherwise, update dp[i] as
        // sum of dp[i + K * arr[i]] and arr[i]
        else {
            dp[i] = dp[i + K * arr[i]] + arr[i];
        }
 
        // Update the overall maximum sum
        maxval = Math.max(maxval, dp[i]);
    }
 
    // Print the answer
    System.out.print(maxval);
}
 
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 1, 3, 1, 2 };
    int N = arr.length;
    int K = 3;
    maxSum(arr, N, K);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python 3 program for the above approach
 
# Function to find the maximum sum
# possible by jumps of length
# i + K*arr[i] from any i-th index
def maxSum(arr, N, K):
    # Initialize an array dp[]
    dp = [0 for i in range(N+2)]
 
    # Stores the maximum sum
    maxval = 0
 
    # Iterate over the range [N-1, 0]
    i = N - 1
    while(i >= 0):
        # If length of the jump exceeds N
        if ((i + K * arr[i]) >= N):
            # Set dp[i] as arr[i]
            dp[i] = arr[i]
 
        # Otherwise, update dp[i] as
        # sum of dp[i + K * arr[i]] and arr[i]
        else:
            dp[i] = dp[i + K * arr[i]] + arr[i]
 
        # Update the overall maximum sum
        maxval = max(maxval, dp[i])
        i -= 1
 
    # Print the answer
    print(maxval)
 
# Driver Code
if __name__ == '__main__':
    arr =  [2, 1, 3, 1, 2]
    N = len(arr)
    K = 3
    maxSum(arr, N, K)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to find the maximum sum
// possible by jumps of length
// i + K*arr[i] from any i-th index
static void maxSum(int[] arr, int N, int K)
{
   
    // Initialize an array dp[]
    int[] dp = new int[N + 2];
    for(int i = 0; i< N+2; i++)
    {
        dp[i] = 0;
    }
 
    // Stores the maximum sum
    int maxval = 0;
 
    // Iterate over the range [N-1, 0]
    for (int i = N - 1; i >= 0; i--)
    {
 
        // If length of the jump exceeds N
        if ((i + K * arr[i]) >= N)
        {
 
            // Set dp[i] as arr[i]
            dp[i] = arr[i];
        }
 
        // Otherwise, update dp[i] as
        // sum of dp[i + K * arr[i]] and arr[i]
        else {
            dp[i] = dp[i + K * arr[i]] + arr[i];
        }
 
        // Update the overall maximum sum
        maxval = Math.Max(maxval, dp[i]);
    }
 
    // Print the answer
    Console.WriteLine(maxval);
}
 
// Driver Code
static public void Main()
{
    int[] arr = { 2, 1, 3, 1, 2 };
    int N = arr.Length;
    int K = 3;
    maxSum(arr, N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live