给定一个排序后的数组ARR大小为N的[]中,任务是检查是否有可能通过跳跃或者ARR达到从ARR [1]在给定阵列的端[I] + K – 1,ARR [I] + k,或每一步的arr [i] + k + 1 ,其中k表示上一步中跳跃的索引数。最初考虑K = 1 。如果可能,请打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {0, 1, 3, 5, 6, 8, 12, 17}
Output: Yes
Explanation:
Step 1: Take (k + 1 = 2) steps to move to index containing A[1] + 2 = 3
Step 2: Take (k = 2) steps to move to index containing A[2] + 2 = 5.
Step 3: Take (k + 1 = 3) steps to move to index containing A[3] + 3 = 8
Step 4: Take (k + 1 = 4) steps to move to index containing A[5] + 4 = 12
Step 4: Take (k + 1 = 5) steps to move to index containing A[6] + 5 = 17
Since the last array index contains 17, the end of the array is reached.
Input: arr[] = {0, 1, 2, 3, 4, 8, 9, 11}
Output: No
天真的方法:这个想法是使用递归。从索引i递归移动到值为A [i] + K – 1,A [i] + K或A [i] + K + 1的索引,并检查是否有可能到达终点。如果存在到达终点的任何路径,则打印“是”,否则打印“否” 。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用动态编程。创建一个二维表备忘[N] [N]以存储记忆的结果。对于任何索引(i,j) , memo [i] [j]表示是否有可能从索引i移至结尾(N-1),并且先前已执行j步。 memo [i] [j] = 1表示可能,而memo [i] [j] = 0表示不可能。请按照以下步骤解决问题:
- 创建一个大小为N * N的备注表memo [] [] 。
- 对于任何索引(i,j) ,请按照以下说明更新memo [] []表:
- 如果i等于(N – 1) ,则在到达终点位置时返回1。
- 如果memo [i] [j]已被计算,则返回memo [i] [j] 。
- 检查具有A [i] + j,A [i] + j – 1或A [i] + j + 1值的任何索引,然后递归检查是否可以到达终点。
- 将值存储在memo [i] [j]中并返回该值。
- 如果有可能到达终点,请打印“是” 。
- 否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define N 8
// Utility function to check if it is
// possible to move from index 1 to N-1
int check(int memo[][N], int i,
int j, int* A)
{
// memo[i][j]: Stores if it is
// possible to move from index i
// to end(N-1) previously j steps
// Successfully reached end index
if (i == N - 1)
return 1;
// memo[i][j] is already calculated
if (memo[i][j] != -1)
return memo[i][j];
// Check if there is any index
// having value of A[i]+j-1,
// A[i]+j or A[i]+j+1
int flag = 0, k;
for (k = i + 1; k < N; k++) {
// If A[k] > A[i] + j + 1,
// can't make a move further
if (A[k] - A[i] > j + 1)
break;
// It's possible to move A[k]
if (A[k] - A[i] >= j - 1
&& A[k] - A[i] <= j + 1)
// Check is it possible to
// move from index k
// having previously taken
// A[k] - A[i] steps
flag = check(memo, k,
A[k] - A[i], A);
// If yes then break the loop
if (flag)
break;
}
// Store value of flag in memo
memo[i][j] = flag;
// Return memo[i][j]
return memo[i][j];
}
// Function to check if it is possible
// to move from index 1 to N-1
void checkEndReach(int A[], int K)
{
// Stores the memoized state
int memo[N][N];
// Initialize all values as -1
memset(memo, -1, sizeof(memo));
// Initially, starting index = 1
int startIndex = 1;
// Function call
if (check(memo, startIndex, K, A))
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
int A[] = { 0, 1, 3, 5, 6,
8, 12, 17 };
int K = 1;
// Function Call
checkEndReach(A, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
static int N = 8;
// Utility function to check if it is
// possible to move from index 1 to N-1
static int check(int[][] memo, int i,
int j, int[] A)
{
// memo[i][j]: Stores if it is
// possible to move from index i
// to end(N-1) previously j steps
// Successfully reached end index
if (i == N - 1)
return 1;
// memo[i][j] is already calculated
if (memo[i][j] != -1)
return memo[i][j];
// Check if there is any index
// having value of A[i]+j-1,
// A[i]+j or A[i]+j+1
int flag = 0, k;
for(k = i + 1; k < N; k++)
{
// If A[k] > A[i] + j + 1,
// can't make a move further
if (A[k] - A[i] > j + 1)
break;
// It's possible to move A[k]
if (A[k] - A[i] >= j - 1 &&
A[k] - A[i] <= j + 1)
// Check is it possible to
// move from index k
// having previously taken
// A[k] - A[i] steps
flag = check(memo, k, A[k] - A[i], A);
// If yes then break the loop
if (flag != 0)
break;
}
// Store value of flag in memo
memo[i][j] = flag;
// Return memo[i][j]
return memo[i][j];
}
// Function to check if it is possible
// to move from index 1 to N-1
static void checkEndReach(int A[], int K)
{
// Stores the memoized state
int[][] memo = new int[N][N];
// Initialize all values as -1
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
memo[i][j] = -1;
}
}
// Initially, starting index = 1
int startIndex = 1;
// Function call
if (check(memo, startIndex, K, A) != 0)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String[] args)
{
int[] A = { 0, 1, 3, 5, 6, 8, 12, 17 };
int K = 1;
// Function Call
checkEndReach(A, K);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
N = 8
# Utility function to check if it is
# possible to move from index 1 to N-1
def check(memo, i, j, A):
# memo[i][j]: Stores if it is
# possible to move from index i
# to end(N-1) previously j steps
# Successfully reached end index
if (i == N - 1):
return 1
# memo[i][j] is already calculated
if (memo[i][j] != -1):
return memo[i][j]
# Check if there is any index
# having value of A[i]+j-1,
# A[i]+j or A[i]+j+1
flag = 0
for k in range(i + 1, N):
# If A[k] > A[i] + j + 1,
# can't make a move further
if (A[k] - A[i] > j + 1):
break
# It's possible to move A[k]
if (A[k] - A[i] >= j - 1 and
A[k] - A[i] <= j + 1):
# Check is it possible to
# move from index k
# having previously taken
# A[k] - A[i] steps
flag = check(memo, k,
A[k] - A[i], A)
# If yes then break the loop
if (flag != 0):
break
# Store value of flag in memo
memo[i][j] = flag
# Return memo[i][j]
return memo[i][j]
# Function to check if it is possible
# to move from index 1 to N-1
def checkEndReach(A, K):
# Stores the memoized state
memo = [[0] * N] * N
# Initialize all values as -1
for i in range(0, N):
for j in range(0, N):
memo[i][j] = -1
# Initially, starting index = 1
startIndex = 1
# Function call
if (check(memo, startIndex, K, A) != 0):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == '__main__':
A = [ 0, 1, 3, 5, 6, 8, 12, 17 ]
K = 1
# Function Call
checkEndReach(A, K)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
static int N = 8;
// Utility function to check if it is
// possible to move from index 1 to N-1
static int check(int[,] memo, int i,
int j, int[] A)
{
// memo[i][j]: Stores if it is
// possible to move from index i
// to end(N-1) previously j steps
// Successfully reached end index
if (i == N - 1)
return 1;
// memo[i][j] is already calculated
if (memo[i, j] != -1)
return memo[i, j];
// Check if there is any index
// having value of A[i]+j-1,
// A[i]+j or A[i]+j+1
int flag = 0, k;
for(k = i + 1; k < N; k++)
{
// If A[k] > A[i] + j + 1,
// can't make a move further
if (A[k] - A[i] > j + 1)
break;
// It's possible to move A[k]
if (A[k] - A[i] >= j - 1 &&
A[k] - A[i] <= j + 1)
// Check is it possible to
// move from index k
// having previously taken
// A[k] - A[i] steps
flag = check(memo, k, A[k] - A[i], A);
// If yes then break the loop
if (flag != 0)
break;
}
// Store value of flag in memo
memo[i, j] = flag;
// Return memo[i][j]
return memo[i, j];
}
// Function to check if it is possible
// to move from index 1 to N-1
static void checkEndReach(int[] A, int K)
{
// Stores the memoized state
int[,] memo = new int[N, N];
// Initialize all values as -1
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
memo[i, j] = -1;
}
}
// Initially, starting index = 1
int startIndex = 1;
// Function call
if (check(memo, startIndex, K, A) != 0)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver Code
public static void Main()
{
int[] A = { 0, 1, 3, 5, 6, 8, 12, 17 };
int K = 1;
// Function Call
checkEndReach(A, K);
}
}
// This code is contributed by akhilsaini
Javascript
Yes
时间复杂度: O(N 3 )
辅助空间: O(N 2 )