给定一个由N 个正整数和一个整数K组成的数组arr[] ,任务是通过从任何第i个节点跳(i + K*arr[i]) ( < N )数组的索引。
例子:
Input: arr[] = {1, 2, 1, 4}, K = 2
Output: 4
Explanation:
Starting index i = 0, the traversal of indices of array is {0, 2}
Hence, the answer is 4.
Input: arr[] = {2, 1, 3, 1, 2}, K = 3
Output: 3
朴素的方法:最简单的方法是在[0, N – 1]范围内为每个可能的起始索引遍历给定数组,并找到通过(i + K*arr[i]的跳转获得的所有和的最大值)来自每个可能的索引i 。在所有遍历之后打印获得的最大和。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:上述方法可以使用动态规划进行优化。这个想法是使用一个辅助数组dp[]使得dp[i]存储通过使用以下递推关系选择i作为起始索引可以获得的总和:
dp[i] = dp[i + K*arr[i]] + arr[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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。