给定一个整数N和一个大小为N的数组arr[ ] ,任务是在给定约束条件下找到到达数组末尾的最大跳跃,即从索引i可以使arr[i]跳跃并到达位置i+arr [我] 。
例子:
Input: N = 5, arr[] = {2, 3, 5, 7, 9}
Output: 12
Explanation:
At index 0 make 2 jumps and move to index 2 and make 5 jumps after that to reach index 7 which is out of the array so total number of jumps is (2+5)=7.
At index 1 make 3+9= 12 jumps
At index 2 make 5 jumps
At index 3 make 7 jumps
At index 4 make 9 jumps
Input: arr[]={2, 2, 1, 2, 3, 3}
Output: 8
方法:思路是用动态规划来解决这个问题。请按照以下步骤解决问题。
- 用 0 初始化大小为N的数组dp 。 dp[i]存储从索引i到达数组末尾所需的跳转次数。此外,将整数ans初始化为 0。
- 遍历数组 从数组的末尾使用 for 循环
- 将arr[i]分配给dp[i],因为arr[i]是从此索引跳转的最小次数。
- 现在初始化一个变量 say j并赋值j = i+arr[i]。
- 如果j的值小于N ,则将dp[j]添加到dp[i] 。
- 将ans的值更新为 max( ans , dp[i] )
- 完成上述步骤后,打印ans的值。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find the maximum
// jumps to reach end of array
void findMaxJumps(int arr[], int N)
{
// Stores the jumps needed
// to reach end from each index
int dp[N] = { 0 };
int ans = 0;
// Traverse the array
for (int i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
// Check if j is less
// than N
if (j < N) {
// Add dp[j] to the
// value of dp[i]
dp[i] = dp[i] + dp[j];
}
// Update the value
// of ans
ans = max(ans, dp[i]);
}
// Print the value of ans
cout << ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 5, 7, 9 };
int N = sizeof(arr) / sizeof(arr[0]);
findMaxJumps(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG {
// Function to find the maximum
// jumps to reach end of array
static void findMaxJumps(int arr[], int N)
{
// Stores the jumps needed
// to reach end from each index
int dp[] = new int [N];
int ans = 0;
// Traverse the array
for (int i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
// Check if j is less
// than N
if (j < N) {
// Add dp[j] to the
// value of dp[i]
dp[i] = dp[i] + dp[j];
}
// Update the value
// of ans
ans = Math.max(ans, dp[i]);
}
// Print the value of ans
System.out.println(ans);
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 2, 3, 5, 7, 9 };
int N = arr.length;
findMaxJumps(arr, N);
}
}
// This code is contributed by Dharanendra L V.
Python3
# python 3 code for the above approach
# Function to find the maximum
# jumps to reach end of array
def findMaxJumps(arr, N):
# Stores the jumps needed
# to reach end from each index
dp = [0 for i in range(N)]
ans = 0
# Traverse the array
i = N - 1
while(i >= 0):
dp[i] = arr[i]
j = i + arr[i]
# Check if j is less
# than N
if (j < N):
# Add dp[j] to the
# value of dp[i]
dp[i] = dp[i] + dp[j]
# Update the value
# of ans
ans = max(ans, dp[i])
i -= 1
# Print the value of ans
print(ans)
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 5, 7, 9]
N = len(arr)
findMaxJumps(arr, N)
# This code is contributed by ipg2016107.
C#
// C# code for the above approach
using System;
class GFG {
// Function to find the maximum
// jumps to reach end of array
static void findMaxJumps(int[] arr, int N)
{
// Stores the jumps needed
// to reach end from each index
int[] dp = new int[N];
int ans = 0;
// Traverse the array
for (int i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
// Check if j is less
// than N
if (j < N) {
// Add dp[j] to the
// value of dp[i]
dp[i] = dp[i] + dp[j];
}
// Update the value
// of ans
ans = Math.Max(ans, dp[i]);
}
// Print the value of ans
Console.Write(ans);
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 2, 3, 5, 7, 9 };
int N = arr.Length;
findMaxJumps(arr, N);
}
}
// This code is contributed by ukasp.
Javascript
输出
12
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。