-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclimbing-stairs.js
42 lines (37 loc) · 968 Bytes
/
climbing-stairs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Source: https://leetcode.com/problems/climbing-stairs/
* Tags: [Dynamic Programming]
* Level: Easy
* Title: Climbing Stairs
* Auther: @imcoddy
* Content: You are climbing a stair case. It takes n steps to reach to the top.
*
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
*/
/**
* @param {number} n
* @return {number}
*/
/**
* Memo: for s[i], it can come from s[i-1] with one step or s[i-2] with two steps.
* Runtime: 131ms
* Tests: 45 test cases passed
* Rank: A
*/
var climbStairs = function(n) {
var s = [0, 1, 2];
if (n <= 2) {
return s[n];
}
for (var i = 3; i <= n; i++) {
s[i] = s[i - 1] + s[i - 2];
}
return s[n];
};
console.log(climbStairs(3));
console.log(climbStairs(4));
console.log(climbStairs(5));
var should = require('should');
climbStairs(3).should.equal(3);
climbStairs(4).should.equal(5);
climbStairs(5).should.equal(8);