-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathclimbingStairs.js
More file actions
82 lines (77 loc) · 2.14 KB
/
climbingStairs.js
File metadata and controls
82 lines (77 loc) · 2.14 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Returns the number of distinct ways to climb to the top of a staircase with n steps,
* where each time you can climb 1 or 2 steps.
* Optimized DP (Fibonacci) approach.
* @param {number} n - Number of steps
* @returns {number}
*/
function climbStairsOptimized(n) {
if (typeof n !== "number" || n < 0 || !Number.isInteger(n))
throw new Error("Input must be a non-negative integer");
if (n <= 2) return n;
let first = 1,
second = 1;
for (let i = 2; i <= n; i++) {
[first, second] = [second, first + second];
}
return second;
}
/**
* Returns the number of distinct ways to climb to the top of a staircase with n steps,
* using bottom-up DP with an array.
* @param {number} n
* @returns {number}
*/
function climbStairsDP(n) {
if (typeof n !== "number" || n < 0 || !Number.isInteger(n))
throw new Error("Input must be a non-negative integer");
if (n <= 2) return n;
const dp = new Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
/**
* Returns the number of distinct ways to climb to the top of a staircase with n steps,
* using recursion with memoization.
* @param {number} n
* @returns {number}
*/
function climbStairsRecursive(n) {
if (typeof n !== "number" || n < 0 || !Number.isInteger(n))
throw new Error("Input must be a non-negative integer");
const memo = {};
function helper(k) {
if (k <= 2) return k;
if (memo[k]) return memo[k];
return (memo[k] = helper(k - 1) + helper(k - 2));
}
return helper(n);
}
// Test cases
const testCases = [
{ n: 0, expected: 0 },
{ n: 1, expected: 1 },
{ n: 2, expected: 2 },
{ n: 3, expected: 3 },
{ n: 5, expected: 8 },
{ n: 6, expected: 13 },
{ n: 10, expected: 89 },
{ n: 20, expected: 10946 },
];
for (const { n, expected } of testCases) {
console.log(
`n = ${n} | Optimized: ${climbStairsOptimized(n)} | DP: ${climbStairsDP(
n
)} | Recursive: ${climbStairsRecursive(n)} | Expected: ${expected}`
);
}
// Edge case: invalid input
try {
climbStairsOptimized(-1);
} catch (e) {
console.log("Error (as expected for n = -1):", e.message);
}