Skip to content

Commit 7c4fa15

Browse files
Jaehyeon Robert HanJaehyeon Robert Han
authored andcommitted
limbing-stairs solution
1 parent 0591309 commit 7c4fa15

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

climbing-stairs/Zioq.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var climbStairs = function(n) {
6+
// 1 -> 1
7+
// 2 -> (1+1) | 2
8+
// 3 -> (1+1+1) | (1+2) | (2+1)
9+
// 4 -> (1+1+1+1) | (2+1+1) | (1+2+1) | (1+1+2) | (1+2+2)
10+
// 5 -> (1+1+1+1+1) | (2+1+1+1) | (1+2+1+1) | (1+1+2+1) | (1+1+1+2) | (1+2+2) | (2+1+2) | (2+2+1)
11+
if(n <=3) return n
12+
13+
let a = 0;
14+
let b = 1;
15+
let c = 0;
16+
17+
for(let i=0; i<n; i++) {
18+
c = a+b;
19+
a = b;
20+
b = c;
21+
}
22+
return b;
23+
};
24+
25+
26+
console.log(climbStairs(2))
27+
console.log(climbStairs(3))
28+
console.log(climbStairs(4))
29+
console.log(climbStairs(5))

0 commit comments

Comments
 (0)