-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Climbing_stairs.js
49 lines (41 loc) · 1.07 KB
/
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
43
44
45
46
47
48
49
/*
Problem statement :
A Person is at the bottom of n stairs .
To reach the top he can climb either 1 or 2 stairs at a time.
Find the number of ways he can reach the top
*/
function climbing_stairs(n) {
let dp = new Array(n);
dp[0] = 1
dp[1] = 1
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
//For reading input
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const getLine = (function () {
const getLineGen = (async function* () {
for await (const line of rl) {
yield line;
}
})();
return async () => ((await getLineGen.next()).value);
})();
const main = async () => {
console.log("Enter the number stairs");
let n = Number(await getLine());
console.log("The number of ways to climb the stairs is : " + climbing_stairs(n));
process.exit(0);
};
main();
/*
Sample I/O:
Enter the number stairs
5
The number of ways to climb the stairs is : 8
Time complexity : O(n)
Space complexity : O(n)
*/