Skip to main content

Climb Stairs

1. You are given a number n, representing the number of stairs in a staircase.

2. You are on the 0th step and are required to climb to the top.

3. In one move, you are allowed to climb 1, 2 or 3 stairs.

4. You are required to print the number of different paths via which you can climb to the top.

Input Format

A number n

Output Format

A number representing the number of ways to climb the stairs from 0 to top.


Constraints

0 <= n <= 20

Sample Input

4

Sample Output

7


Solution:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(WaysUp(n));
    }

    public static int WaysUp(int n){
        if(n == 0)  return 0;
        else if(n<0)    return -1;
        int[] A = new int[n+1];
        A[0] = 1;
        for(int i=1;i<=n;i++){
            A[i] = A[i-1];
            if(i>= 2)
                A[i] += A[i-2];
            if(i>=3)
                A[i] += A[i-3];
        }
        return A[n];
    }
}

Comments

Must Read:

RDBMS Data Definition Language | Create Sales_info table

  Write a query to create Sales_info with constraints mentioned. Refer the below schema  Result Description Summary of tests +------------------------------+ | 3 tests run / 3 test passed | +------------------------------+

Algorithm Analysis and Design Concepts Data Structure and Algorithms Post-Quiz

Algorithm Analysis and Design Concepts       Data Structure and Algorithms            Post-Quiz  

Algorithm Analysis and Design Concepts Analysis of Algorithms Post-Quiz

 Algorithm Analysis and Design Concepts       Analysis of Algorithms            Post-Quiz  

RDBMS Data Definition Language | Change the name of the table Sales Info

  RDBMS  Data Definition Language  Change the name of the table Sales Info Write an SQL statement to rename the table Sales Info to Sales_Information. (Hint: use alter command to rename the table name) Evaluation Result: Result Description Summary of tests +------------------------------+ | 1 tests run / 1 test passed | +------------------------------+

Mock 3 Slot3 - Quiz - 21st July Quiz Quiz

Mock 3 Slot3        Quiz - 21st July          Important Questions

Logic Development | Object Oriented Programming Post Quiz

 Logic Development | Object-Oriented Programming | Post Quiz

Programming using Java Hands On - Control Structures | Print Username

  Print Username Jeffy, who is going to complete the higher education in this year needs to create a simple application which accept the name of a person and welcome them with a message along with their name. She wants to read the data using the class "Scanner". Implement this scenario using Java. Sample Input 1:   Enter the name: Johson Sample Output 1: Welcome Johnson. Sample Input 2:   Enter the name: Stain Polson Sample Output 2: Welcome Stain Polson. Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 2 tests run/ 2 tests passed | +------------------------------+

Logic Development | Object Oriented Programming Pre Quiz

 

Zig Zag Pattern

  /* @ToDo     Zig Zag Pattern         *               *                *       *       *       *        *               *               *        */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE          freopen ( "../asset/input.txt" , "r" , stdin );          freopen (...

Subscribe to Get's Answer by Email