📜  寻找在楼梯案例中达到第K个台阶的方法数量

📅  最后修改于: 2021-04-26 19:03:34             🧑  作者: Mango

给定大小为N的数组arr []和整数K。数组代表楼梯中折断的台阶。一个人无法达到断断续续的步伐。任务是找到可以在任何位置采取最大长度为2的台阶时从0开始到达楼梯中K台阶的方法的数量。答案可能非常大。因此,以模10 9 + 7的形式打印答案。

例子:

方法:可以使用动态编程解决此问题。创建DP []数组,其中DP [i]于将存储的路的数目达到i步骤和递推关系将DP [I] = DP [I – 1] + DP [I – 2]仅当i步骤没有另外0破碎。最终答案将是dp [K]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int MOD = 1000000007;
  
// Function to return the number
// of ways to reach the kth step
int number_of_ways(int arr[], int n, int k)
{
    if (k == 1)
        return 1;
  
    // Create the dp array
    int dp[k + 1];
    memset(dp, -1, sizeof dp);
  
    // Broken steps
    for (int i = 0; i < n; i++)
        dp[arr[i]] = 0;
  
    dp[0] = 1;
    dp[1] = (dp[1] == -1) ? 1 : dp[1];
  
    // Calculate the number of ways for
    // the rest of the positions
    for (int i = 2; i <= k; ++i) {
  
        // If it is a blocked position
        if (dp[i] == 0)
            continue;
  
        // Number of ways to get to the ith step
        dp[i] = dp[i - 1] + dp[i - 2];
  
        dp[i] %= MOD;
    }
  
    // Return the required answer
    return dp[k];
}
  
// Driver code
int main()
{
    int arr[] = { 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 6;
  
    cout << number_of_ways(arr, n, k);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
    static final int MOD = 1000000007; 
      
    // Function to return the number 
    // of ways to reach the kth step 
    static int number_of_ways(int arr[], 
                              int n, int k) 
    { 
        if (k == 1) 
            return 1; 
      
        // Create the dp array 
        int dp[] = new int[k + 1]; 
          
        int i;
          
        for(i = 0; i < k + 1; i++)
            dp[i] = -1 ;
  
        // Broken steps 
        for (i = 0; i < n; i++) 
            dp[arr[i]] = 0; 
      
        dp[0] = 1; 
        dp[1] = (dp[1] == -1) ? 1 : dp[1]; 
      
        // Calculate the number of ways for 
        // the rest of the positions 
        for (i = 2; i <= k; ++i)
        { 
      
            // If it is a blocked position 
            if (dp[i] == 0) 
                continue; 
      
            // Number of ways to get to the ith step 
            dp[i] = dp[i - 1] + dp[i - 2]; 
      
            dp[i] %= MOD; 
        } 
      
        // Return the required answer 
        return dp[k]; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int arr[] = { 3 }; 
        int n = arr.length; 
        int k = 6; 
      
        System.out.println(number_of_ways(arr, n, k)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
MOD = 1000000007; 
  
# Function to return the number 
# of ways to reach the kth step 
def number_of_ways(arr, n, k) : 
  
    if (k == 1) :
        return 1; 
  
    # Create the dp array 
    dp = [-1] * (k + 1); 
  
    # Broken steps 
    for i in range(n) :
        dp[arr[i]] = 0; 
  
    dp[0] = 1; 
    dp[1] = 1 if (dp[1] == -1) else dp[1]; 
  
    # Calculate the number of ways for 
    # the rest of the positions 
    for i in range(2, k + 1) : 
  
        # If it is a blocked position 
        if (dp[i] == 0) :
            continue; 
  
        # Number of ways to get to the ith step 
        dp[i] = dp[i - 1] + dp[i - 2]; 
  
        dp[i] %= MOD; 
      
    # Return the required answer 
    return dp[k]; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 3 ]; 
    n = len(arr); 
    k = 6; 
  
    print(number_of_ways(arr, n, k)); 
  
# This code is contributed by kanugargng


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
    static readonly int MOD = 1000000007; 
      
    // Function to return the number 
    // of ways to reach the kth step 
    static int number_of_ways(int []arr, 
                              int n, int k) 
    { 
        if (k == 1) 
            return 1; 
      
        // Create the dp array 
        int []dp = new int[k + 1]; 
          
        int i; 
          
        for(i = 0; i < k + 1; i++) 
            dp[i] = -1 ; 
  
        // Broken steps 
        for (i = 0; i < n; i++) 
            dp[arr[i]] = 0; 
      
        dp[0] = 1; 
        dp[1] = (dp[1] == -1) ? 1 : dp[1]; 
      
        // Calculate the number of ways for 
        // the rest of the positions 
        for (i = 2; i <= k; ++i) 
        { 
      
            // If it is a blocked position 
            if (dp[i] == 0) 
                continue; 
      
            // Number of ways to get to the ith step 
            dp[i] = dp[i - 1] + dp[i - 2]; 
      
            dp[i] %= MOD; 
        } 
      
        // Return the required answer 
        return dp[k]; 
    } 
      
    // Driver code 
    public static void Main (String[] args) 
    { 
        int []arr = { 3 }; 
        int n = arr.Length; 
        int k = 6; 
      
        Console.WriteLine(number_of_ways(arr, n, k)); 
    } 
}
  
// This code is contributed by PrinciRaj1992


输出:
4