Array ( [0] => [1] => questions [2] => Codevita-Previous-Questions [3] => Common-Steps ) Codevita Previous Questions | Common Steps | THE INQUISITIVE





Common Steps

LEVEL:Beginner

Description

A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.

Input Format

A single integer n denoting number of steps.

Output Format

Total no. of ways child can run up.


Example 1:

Input
4
Output
7
Example 2:

Input
3
Output
4
Example 3:

Input
10
Output
274

oops

Login to see Discussion




Approach

This was a recursion problem. But recursion took time, so dynamic programming can be applied using the steps named list. No. of steps for a number n can be determined by n-1, n-2, n-3. All the results were calculated by the formula and stored for later use. So that again and again already calculated values need not be calculated again.


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution