Skip to content

Commit 6275b70

Browse files
Create ClimbingStairs.js (TheAlgorithms#379)
* Create ClimbingStairs.js * Update ClimbingStairs.js Co-authored-by: vinayak <[email protected]>
1 parent af081a2 commit 6275b70

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Dynamic-Programming/ClimbingStairs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* You are climbing a stair case. It takes n steps to reach to the top.
3+
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
4+
*/
5+
6+
const climbStairs = (n) => {
7+
let prev = 0
8+
let cur = 1
9+
let temp
10+
11+
for (let i = 0; i < n; i++) {
12+
temp = prev
13+
prev = cur
14+
cur += temp
15+
}
16+
return cur
17+
}
18+
19+
const main = () => {
20+
const number = 5
21+
22+
console.log('Number of ways to climb ' + number + ' stairs in ' + climbStairs(5))
23+
}
24+
25+
// testing
26+
main()

0 commit comments

Comments
 (0)