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

📅  最后修改于: 2021-05-13 23:51:51             🧑  作者: Mango

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

例子:

天真的方法:最简单的方法是遍历[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.


输出:
3

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