Skip to content

visual walkthrough

Climbing Stairs

EasyFibonacci-style RecurrenceReported at: AmazonMicrosoftGoogle+4

The 'hello world' of DP.

Solve on LeetCode

The idea

To stand on step n you either came from step n − 1 or from step n − 2. So the number of ways to reach n is the sum of the ways to reach those two steps.

Written as plain recursion this repeats the same sub-questions again and again. Storing each answer the first time turns an exponential amount of work into a single pass.

Complexity

approachtimespace
Plain recursionO(2ⁿ)O(n)
Dynamic programmingO(n)O(n)

More walkthroughs