📌  相关文章
📜  通过至多 K 次跳跃到达 N 层的方法数

📅  最后修改于: 2021-09-17 07:33:05             🧑  作者: Mango

给定 N 个楼梯。还给出了一个人在一次飞跃(K)中最多可以完成的步数。任务是找到一个(只考虑组合)可以从底层以 K 次或更少的跳跃爬到建筑物顶部的可能方式的数量。
例子:

combo[i]是到达第i 层的方法数。因此,通过跳跃 ij 从组合 [j] 到达组合 [i] 的方法数量将是组合 [i] += 组合 [j]。因此迭代所有可能的跳跃,并为每个可能的跳跃不断将可能的组合添加到组合数组中。最终答案将存储在组合 [N] 中。
下面是上述方法的实现。

C++
// C++ program to reach N-th stair
// by taking a maximum of K leap
#include 
 
using namespace std;
 
int solve(int N, int K)
{
 
    // elements of combo[] stores the no of
    // possible ways to reach it by all
    // combinations of k leaps or less
 
    int combo[N + 1] = { 0 };
 
    // assuming leap 0 exist and assigning
    // its value to 1 for calculation
    combo[0] = 1;
 
    // loop to iterate over all
    // possible leaps upto k;
    for (int i = 1; i <= K; i++) {
 
        // in this loop we count all possible
        // leaps to reach the jth stair with
        // the help of ith leap or less
        for (int j = 0; j <= N; j++) {
 
            // if the leap is not more than the i-j
            if (j >= i) {
 
                // calculate the value and
                // store in combo[j]
                // to reuse it for next leap
                // calculation for the jth stair
                combo[j] += combo[j - i];
            }
        }
    }
 
    // returns the no of possible number
    // of leaps to reach the top of
    // building of n stairs
    return combo[N];
}
 
// Driver Code
int main()
{
    // N i the no of total stairs
    // K is the value of the greatest leap
    int N = 29;
    int K = 5;
 
    cout << solve(N, K);
 
    solve(N, K);
    return 0;
}


Java
// Java program to reach N-th
// stair by taking a maximum
// of K leap
class GFG
{
static int solve(int N, int K)
{
 
    // elements of combo[] stores
    // the no. of possible ways
    // to reach it by all combinations
    // of k leaps or less
    int[] combo;
    combo = new int[50];
 
    // assuming leap 0 exist
    // and assigning its value
    // to 1 for calculation
    combo[0] = 1;
 
    // loop to iterate over all
    // possible leaps upto k;
    for (int i = 1; i <= K; i++)
    {
 
        // in this loop we count all
        // possible leaps to reach
        // the jth stair with the
        // help of ith leap or less
        for (int j = 0; j <= N; j++)
        {
 
            // if the leap is not
            // more than the i-j
            if (j >= i)
            {
 
                // calculate the value and
                // store in combo[j] to
                // reuse it for next leap
                // calculation for the
                // jth stair
                combo[j] += combo[j - i];
            }
        }
    }
 
    // returns the no of possible
    // number of leaps to reach
    // the top of building of
    // n stairs
    return combo[N];
}
 
// Driver Code
public static void main(String args[])
{
    // N i the no of total stairs
    // K is the value of the
    // greatest leap
    int N = 29;
    int K = 5;
 
    System.out.println(solve(N, K));
 
    solve(N, K);
}
}
 
// This code is contributed
// by ankita_saini


Python 3
# Python3 program to reach N-th stair
# by taking a maximum of K leap
 
def solve(N, K) :
 
    # elements of combo[] stores the no of 
    # possible ways to reach it by all 
    # combinations of k leaps or less
    combo = [0] * (N + 1)
 
    # assuming leap 0 exist and assigning 
    # its value to 1 for calculation
    combo[0] = 1
 
    # loop to iterate over all 
    # possible leaps upto k;
    for i in range(1, K + 1) :
 
        #  in this loop we count all possible 
        # leaps to reach the jth stair with 
        # the help of ith leap or less 
        for j in range(0, N + 1) :
 
            # if the leap is not more than the i-j 
            if j >= i :
 
                # calculate the value and 
                # store in combo[j] 
                # to reuse it for next leap 
                # calculation for the jth stair
                combo[j] += combo[j - i]
 
 
    # returns the no of possible number 
    # of leaps to reach the top of 
    # building of n stairs 
    return combo[N]
   
# Driver Code
if __name__ == "__main__" :
 
    # N i the no of total stairs 
    # K is the value of the greatest leap
    N, K = 29, 5
 
    print(solve(N, K))
 
# This code is contributed by ANKITRAI1


C#
// C# program to reach N-th
// stair by taking a maximum
// of K leap
using System;
 
class GFG
{
static int solve(int N, int K)
{
 
    // elements of combo[] stores
    // the no. of possible ways
    // to reach it by all combinations
    // of k leaps or less
    int[] combo;
    combo = new int[50];
 
    // assuming leap 0 exist
    // and assigning its value
    // to 1 for calculation
    combo[0] = 1;
 
    // loop to iterate over all
    // possible leaps upto k;
    for (int i = 1; i <= K; i++)
    {
 
        // in this loop we count all
        // possible leaps to reach
        // the jth stair with the
        // help of ith leap or less
        for (int j = 0; j <= N; j++)
        {
 
            // if the leap is not
            // more than the i-j
            if (j >= i)
            {
 
                // calculate the value and
                // store in combo[j] to
                // reuse it for next leap
                // calculation for the
                // jth stair
                combo[j] += combo[j - i];
            }
        }
    }
 
    // returns the no of possible
    // number of leaps to reach
    // the top of building of
    // n stairs
    return combo[N];
}
 
// Driver Code
public static void Main()
{
    // N i the no of total stairs
    // K is the value of the
    // greatest leap
    int N = 29;
    int K = 5;
 
    Console.WriteLine(solve(N, K));
    solve(N, K);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP
= $i)
            {
 
                // calculate the value and
                // store in combo[j]
                // to reuse it for next leap
                // calculation for the jth stair
                $combo[$j] += $combo[$j - $i];
            }
        }
    }
 
    // returns the no of possible
    // number of leaps to reach
    // the top of building of n stairs
    return $combo[$N];
}
 
// Driver Code
 
// N i the no of total stairs
// K is the value of the greatest leap
$N = 29;
$K = 5;
 
echo solve($N, $K);
 
solve($N, $K);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>


Javascript


输出:

603

时间复杂度: O(N*K)
辅助空间: O(N)

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