Simran and stairs

Simran is running up a staircase with N steps and can hop(jump) either 1 step, 2 steps, or 3 steps at a time. You have to count, how many possible ways Simran can run-up to the stairs.

Example:

Input:  n = 4
Output: 7

Approach

C++

#include <bits/stdc++.h>
using namespace std;
int countStairs(int n)
{

    int dp[n + 1];
    dp[0] = 1;
    dp[1] = 1;
    if (n == 1)
        return dp[1];
    dp[2] = 2;
    for (int i = 3i <= ni++)
        dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
    return dp[n];
}
int main()
{
    int n = 4;

    int res = 0;
    res = countStairs(n);
    cout << res << "\n";
    return 0;
}


No comments:

Post a Comment