You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1:

Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps

Example 2:

Input: n = 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step

Constraints:

  • 1 <= n <= 45

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 2
    integer :: i, j, k, l, m, n_steps
    integer, dimension(n) :: steps

    ! Example 1
    write (*, "(A)") "Example 1:"
    write (*, "(A, I0)") "Input: ", n
    write (*, "(A, I0)") "Output: ", climb_stairs(n)
    write (*, *)

    ! Example 2
    write (*, "(A)") "Example 2:"
    write (*, "(A, I0)") "Input: ", n
    write (*, "(A, I0)") "Output: ", climb_stairs(n)
    write (*, *)

contains

    function climb_stairs(n) result(num_ways)
        implicit none
        integer, intent(in) :: n
        integer :: num_ways, i

        if (n == 1) then
            num_ways = 1
        else if (n == 2) then
            num_ways = 2
        else
            num_ways = climb_stairs(n - 1) + climb_stairs(n - 2)
        end if
    end function climb_stairs

end program main
βœ… Compiled
❌ Executed
❌ Correct
!-------------------------------------------------------------------------------
! This is a Fortran implementation of the staircase climbing problem.
!
! The program takes a single argument, which is the number of steps to climb.
! It outputs the number of distinct ways to climb to the top.
!
! Example usage:
!   $ ./staircase 2
!   2
!   $ ./staircase 3
!   3
!   $ ./staircase 45
!   184756
!-------------------------------------------------------------------------------

program staircase
    implicit none

    integer :: n, i, j
    integer :: dp(45)

    ! Read the number of steps from the command line argument
    read(*,*) n

    ! Initialize the dp array with the base case
    dp(1) = 1
    dp(2) = 2

    ! Fill in the rest of the dp array
    do i = 3, n
        dp(i) = 0
        do j = 1, i-1
            dp(i) = dp(i) + dp(j) * dp(i-j)
        end do
    end do

    ! Output the result
    write(*,*) dp(n)

end program staircase
🌐 Data from online sources
def climbStairs(n):
    if n <= 2:
        return n
    a, b = 1, 2
    for _ in range(3, n+1):
        a, b = b, a + b
    return b
This problem can be solved using dynamic programming. The number of ways to reach the top can be calculated by finding the sum of the ways to reach the previous two steps. We start with the base cases where `n` is 1 or 2, and then iteratively build up the solution. In each iteration, we update `a` and `b` with the new number of ways to reach steps `n-2` and `n-1` respectively, and keep calculating the number of ways to reach step `n` using `a + b`. This continues until we reach the top and return the final result.
🌐 Data from online sources
int climbStairs(int n) {
    if(n <= 2) return n;
    int a = 1, b = 2, c;
    for(int i = 3; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}
This problem can be solved using dynamic programming. The number of ways to reach the top can be calculated by finding the sum of the ways to reach the previous two steps. We start with the base cases where `n` is 1 or 2, and then iteratively build up the solution. In each iteration, we update `a` and `b` with the new number of ways to reach steps `n-2` and `n-1` respectively, and keep calculating the number of ways to reach step `n` using `a + b`. This continues until we reach the top and return the final result.