All problems

Problem 20

Climbing Stairs

Easy · 10 pts · Dynamic Programming, Math

Problem

You are climbing a staircase with N steps. Each move climbs either 1 step or 2 steps. Count the number of different sequences of moves that take you exactly to the top step. Two ways are different if the order of moves differs: 1 + 2 and 2 + 1 are two separate ways to climb 3 steps. To reach step N, your last move came either from step N - 1 or from step N - 2. So ways(N) = ways(N - 1) + ways(N - 2), with ways(1) = 1 and ways(2) = 2. This is the Fibonacci pattern, and a simple loop computes it.

Input

A single integer N.

Output

Print the number of distinct ways to reach the top.

Constraints

1 <= N <= 45

Example input

4

Example output

5

Explanation

For N = 4 there are 5 ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. Using the recurrence: ways(3) = 2 + 1 = 3 and ways(4) = 3 + 2 = 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Longest Common Prefix Next: Sum of Array