📌  相关文章
📜  求到达楼梯第 K 步的方法数

📅  最后修改于: 2021-09-17 06:53:21             🧑  作者: 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


Javascript


输出:
4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程