📜  计算数组中的路径

📅  最后修改于: 2021-04-27 21:24:00             🧑  作者: Mango

给定一个数组A ,该数组A由大小为N的正整数组成。如果数组中索引为i的元素为K,则可以在索引范围(i + 1)(i + K)之间跳转。
任务是找到使用模块10 9 + 7到达终点的可能方法的数量。

起始位置被视为索引0

例子:

天真的方法:我们可以形成一个递归结构来解决该问题。

令F [i]表示从索引i开始的路径数,如果元素A [i]K,则在每个索引i处执行跳转的方式总数为:

F(i) = F(i+1) + F(i+2) +...+ F(i+k), where i + k <= n, 
where F(n) = 1

通过使用此递归公式,我们可以解决问题:

C++
// C++ implementation of
// the above approach
#include 
using namespace std;
  
const int mod = 1e9 + 7;
  
// Find the number of ways
// to reach the end
int ways(int i, int arr[], int n)
{
    // Base case
    if (i == n - 1)
        return 1;
  
    int sum = 0;
  
    // Recursive structure
    for (int j = 1;
         j + i < n && j <= arr[i];
         j++) {
        sum += (ways(i + j,
                     arr, n))
               % mod;
        sum %= mod;
    }
  
    return sum % mod;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 3, 1, 4, 3 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << ways(0, arr, n) << endl;
  
    return 0;
}


Java
// Java implementation of
// the above approach
import java.io.*;
  
class GFG
{
static int mod = 1000000000;
  
// Find the number of ways
// to reach the end
static int ways(int i, 
                int arr[], int n)
{
    // Base case
    if (i == n - 1)
        return 1;
  
    int sum = 0;
  
    // Recursive structure
    for (int j = 1; j + i < n && 
                    j <= arr[i]; j++)
    {
        sum += (ways(i + j,
                     arr, n)) % mod;
        sum %= mod;
    }
    return sum % mod;
}
  
// Driver code
public static void main (String[] args)
{
    int arr[] = { 5, 3, 1, 4, 3 };
      
    int n = arr.length;
  
    System.out.println (ways(0, arr, n));
}
}
  
// This code is contributed by ajit


Python3
# Python3 implementation of
# the above approach
  
mod = 1e9 + 7;
  
# Find the number of ways
# to reach the end
def ways(i, arr, n):
      
    # Base case
    if (i == n - 1):
        return 1;
  
    sum = 0;
  
    # Recursive structure
    for j in range(1, arr[i] + 1):
        if(i + j < n):
            sum += (ways(i + j, arr, n)) % mod;
            sum %= mod;
  
    return int(sum % mod);
  
# Driver code
if __name__ == '__main__':
    arr = [5, 3, 1, 4, 3];
  
    n = len(arr);
  
    print(ways(0, arr, n));
  
# This code is contributed by PrinciRaj1992


C#
// C# implementation of
// the above approach
using System;
      
class GFG
{
static int mod = 1000000000;
  
// Find the number of ways
// to reach the end
static int ways(int i, 
                int []arr, int n)
{
    // Base case
    if (i == n - 1)
        return 1;
  
    int sum = 0;
  
    // Recursive structure
    for (int j = 1; j + i < n && 
                    j <= arr[i]; j++)
    {
        sum += (ways(i + j,
                     arr, n)) % mod;
        sum %= mod;
    }
    return sum % mod;
}
  
// Driver code
public static void Main (String[] args)
{
    int []arr = { 5, 3, 1, 4, 3 };
      
    int n = arr.Length;
  
    Console.WriteLine(ways(0, arr, n));
}
}
  
// This code is contributed by 29AjayKumar


C++
// C++ implementation
#include 
using namespace std;
  
const int mod = 1e9 + 7;
  
// find the number of ways to reach the end
int ways(int arr[], int n)
{
    // dp to store value
    int dp[n + 1];
  
    // base case
    dp[n - 1] = 1;
  
    // Bottom up dp structure
    for (int i = n - 2; i >= 0; i--) {
        dp[i] = 0;
  
        // F[i] is dependent of
        // F[i+1] to F[i+k]
        for (int j = 1; ((j + i) < n
                         && j <= arr[i]);
             j++) {
            dp[i] += dp[i + j];
            dp[i] %= mod;
        }
    }
  
    // Return value of dp[0]
    return dp[0] % mod;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 3, 1, 4, 3 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << ways(arr, n) % mod << endl;
    return 0;
}


Java
// Java implementation of above approach
class GFG 
{
    static final int mod = (int)(1e9 + 7); 
      
    // find the number of ways to reach the end 
    static int ways(int arr[], int n) 
    { 
        // dp to store value 
        int dp[] = new int[n + 1]; 
      
        // base case 
        dp[n - 1] = 1; 
      
        // Bottom up dp structure 
        for (int i = n - 2; i >= 0; i--) 
        { 
            dp[i] = 0; 
      
            // F[i] is dependent of 
            // F[i+1] to F[i+k] 
            for (int j = 1; ((j + i) < n && 
                              j <= arr[i]); j++)
            { 
                dp[i] += dp[i + j]; 
                dp[i] %= mod; 
            } 
        } 
      
        // Return value of dp[0] 
        return dp[0] % mod; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 5, 3, 1, 4, 3 }; 
      
        int n = arr.length; 
      
        System.out.println(ways(arr, n) % mod); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of above approach
mod = 10**9 + 7
  
# find the number of ways to reach the end 
def ways(arr, n):
      
    # dp to store value 
    dp = [0] * (n + 1)
      
    # base case 
    dp[n - 1] = 1
      
    # Bottom up dp structure 
    for i in range(n - 2, -1, -1):
        dp[i] = 0
          
        # F[i] is dependent of 
        # F[i + 1] to F[i + k] 
        j = 1
        while((j + i) < n and j <= arr[i]):
            dp[i] += dp[i + j] 
            dp[i] %= mod 
            j += 1
      
    # Return value of dp[0] 
    return dp[0] % mod 
  
# Driver code 
arr = [5, 3, 1, 4, 3 ]
n = len(arr) 
print(ways(arr, n) % mod)
  
# This code is contributed by SHUBHAMSINGH10


C#
// C# implementation of above approach
using System;
      
class GFG 
{
    static readonly int mod = (int)(1e9 + 7); 
      
    // find the number of ways to reach the end 
    static int ways(int []arr, int n) 
    { 
        // dp to store value 
        int []dp = new int[n + 1]; 
      
        // base case 
        dp[n - 1] = 1; 
      
        // Bottom up dp structure 
        for (int i = n - 2; i >= 0; i--) 
        { 
            dp[i] = 0; 
      
            // F[i] is dependent of 
            // F[i+1] to F[i+k] 
            for (int j = 1; ((j + i) < n && 
                              j <= arr[i]); j++)
            { 
                dp[i] += dp[i + j]; 
                dp[i] %= mod; 
            } 
        } 
      
        // Return value of dp[0] 
        return dp[0] % mod; 
    } 
      
    // Driver code 
    public static void Main (String[] args)
    { 
        int []arr = { 5, 3, 1, 4, 3 }; 
      
        int n = arr.Length; 
      
        Console.WriteLine(ways(arr, n) % mod); 
    } 
}
  
// This code is contributed by Rajput-Ji


输出:
6

高效方法:在以前的方法中,有些计算不只一次进行。最好将这些值存储在dp数组中,并且dp [i]将存储从索引i开始到数组末尾的路径数。

因此,dp [0]将是该问题的解决方案。

下面是该方法的实现:

C++

// C++ implementation
#include 
using namespace std;
  
const int mod = 1e9 + 7;
  
// find the number of ways to reach the end
int ways(int arr[], int n)
{
    // dp to store value
    int dp[n + 1];
  
    // base case
    dp[n - 1] = 1;
  
    // Bottom up dp structure
    for (int i = n - 2; i >= 0; i--) {
        dp[i] = 0;
  
        // F[i] is dependent of
        // F[i+1] to F[i+k]
        for (int j = 1; ((j + i) < n
                         && j <= arr[i]);
             j++) {
            dp[i] += dp[i + j];
            dp[i] %= mod;
        }
    }
  
    // Return value of dp[0]
    return dp[0] % mod;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 3, 1, 4, 3 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << ways(arr, n) % mod << endl;
    return 0;
}

Java

// Java implementation of above approach
class GFG 
{
    static final int mod = (int)(1e9 + 7); 
      
    // find the number of ways to reach the end 
    static int ways(int arr[], int n) 
    { 
        // dp to store value 
        int dp[] = new int[n + 1]; 
      
        // base case 
        dp[n - 1] = 1; 
      
        // Bottom up dp structure 
        for (int i = n - 2; i >= 0; i--) 
        { 
            dp[i] = 0; 
      
            // F[i] is dependent of 
            // F[i+1] to F[i+k] 
            for (int j = 1; ((j + i) < n && 
                              j <= arr[i]); j++)
            { 
                dp[i] += dp[i + j]; 
                dp[i] %= mod; 
            } 
        } 
      
        // Return value of dp[0] 
        return dp[0] % mod; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 5, 3, 1, 4, 3 }; 
      
        int n = arr.length; 
      
        System.out.println(ways(arr, n) % mod); 
    } 
}
  
// This code is contributed by AnkitRai01

Python3

# Python3 implementation of above approach
mod = 10**9 + 7
  
# find the number of ways to reach the end 
def ways(arr, n):
      
    # dp to store value 
    dp = [0] * (n + 1)
      
    # base case 
    dp[n - 1] = 1
      
    # Bottom up dp structure 
    for i in range(n - 2, -1, -1):
        dp[i] = 0
          
        # F[i] is dependent of 
        # F[i + 1] to F[i + k] 
        j = 1
        while((j + i) < n and j <= arr[i]):
            dp[i] += dp[i + j] 
            dp[i] %= mod 
            j += 1
      
    # Return value of dp[0] 
    return dp[0] % mod 
  
# Driver code 
arr = [5, 3, 1, 4, 3 ]
n = len(arr) 
print(ways(arr, n) % mod)
  
# This code is contributed by SHUBHAMSINGH10

C#

// C# implementation of above approach
using System;
      
class GFG 
{
    static readonly int mod = (int)(1e9 + 7); 
      
    // find the number of ways to reach the end 
    static int ways(int []arr, int n) 
    { 
        // dp to store value 
        int []dp = new int[n + 1]; 
      
        // base case 
        dp[n - 1] = 1; 
      
        // Bottom up dp structure 
        for (int i = n - 2; i >= 0; i--) 
        { 
            dp[i] = 0; 
      
            // F[i] is dependent of 
            // F[i+1] to F[i+k] 
            for (int j = 1; ((j + i) < n && 
                              j <= arr[i]); j++)
            { 
                dp[i] += dp[i + j]; 
                dp[i] %= mod; 
            } 
        } 
      
        // Return value of dp[0] 
        return dp[0] % mod; 
    } 
      
    // Driver code 
    public static void Main (String[] args)
    { 
        int []arr = { 5, 3, 1, 4, 3 }; 
      
        int n = arr.Length; 
      
        Console.WriteLine(ways(arr, n) % mod); 
    } 
}
  
// This code is contributed by Rajput-Ji
输出:
6

时间复杂度: O(K)