-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathJeehay28.js
41 lines (31 loc) · 1.04 KB
/
Jeehay28.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
/**
* @param {number} n
* @return {number}
*/
// Time Complexity: O(n^2)
// First O(n): From the loop that runs up to n times.
// Second O(n): From the factorial calculations in each iteration.
// overall time complexity is O(n^2).
// Space Complexity: O(n)
// the factorial function's recursion has a space complexity of O(n)
// O(1) means constant space complexity. It implies that the amount of memory used does not grow with the size of the input.
// The other variables in the function only use a constant amount of space, resulting in an O(1) space usage.
var climbStairs = function (n) {
let ways = 0;
let maxStepTwo = Math.floor(n / 2);
const factorial = (num) => {
if (num === 0 || num === 1) {
return 1;
}
for (let i = 2; i <= num; i++) {
return num * factorial(num - 1);
}
};
for (let i = 0; i <= maxStepTwo; i++) {
const stepTwo = i;
const stepOne = n - 2 * i;
const steps = stepTwo + stepOne;
ways += factorial(steps) / (factorial(stepTwo) * factorial(stepOne));
}
return ways;
};