We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4408e31 commit bab1ff4Copy full SHA for bab1ff4
climbing-stairs/yoonthecoder.js
@@ -0,0 +1,17 @@
1
+function climbStairs(n) {
2
+ // if there's only 1 step, return 1
3
+ if (n === 1) return 1;
4
+ // initialize the first prev values (when n=1 and n=0 each)
5
+ let prev1 = 1;
6
+ let prev2 = 1;
7
+ // iterate through step 2 to n
8
+ for (let i = 2; i <= n; i++) {
9
+ let current = prev1 + prev2;
10
+ prev2 = prev1;
11
+ prev1 = current;
12
+ }
13
+ return prev1;
14
+}
15
+
16
+// Time complexity: O(n) - single for loop
17
+// Space Complexity: O(1)
0 commit comments