Algorithms and Data Structures · Middle · Question 42 of 75

Climbing stairs: you can step 1 or 2 stairs at a time. How many ways are there to climb n stairs?

Explanation

The last move is either a single step from stair n - 1 or a double step from n - 2, so the counts add up. With two variables it's O(n) time and O(1) memory.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 42

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz