Oleksii Trekhleb | Javascript algorithms (Recursive staircase problem)
This is a series of books diving deep into the core mechanisms of the JavaScript language.
· 1 phút đọc.
![nhavantuonglai This is a series of books diving deep into the core mechanisms of the JavaScript language.](https://data.nhavantuonglai.com/image/illustrations/cover-nhavantuonglai-com-05286.jpg)
The Problem
There are n
stairs, a person standing at the bottom wants to reach the top. The person can climb either 1
or 2
stairs at a time. Count the number of ways, the person can reach the top.
The Solution
This is an interesting problem because there are several ways of how it may be solved that illustrate different programming paradigms.
Brute Force Recursive Solution - Time:
O(2^n)
; Space:O(1)
Recursive Solution With Memoization - Time:
O(n)
; Space:O(n)
Dynamic Programming Solution - Time:
O(n)
; Space:O(n)
Iterative Solution - Time:
O(n)
; Space:O(1)
## References
![nhavantuonglai nhavantuonglai](https://data.nhavantuonglai.com/image/illustrations/cover-nhavantuonglai-com-0127.jpg)