Leetcode 70 - Climbing stairs

Note: Just like fibonacci numbers.

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

Example 1:

1
2
3
4
5
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
if (n <= 1) return n;
let ans = [];
ans[1] = 1;
ans[2] = 2;
for (let i = 3; i <= n; i++) {
ans[i] = ans[i-1] + ans[i-2];
}
return ans[n];
};