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 cf9d00c commit e061a11Copy full SHA for e061a11
climbing-stairs/taewanseoul.ts
@@ -0,0 +1,26 @@
1
+/**
2
+ * 70. Climbing Stairs
3
+ * You are climbing a staircase. It takes n steps to reach the top.
4
+ * Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5
+ *
6
+ * https://leetcode.com/problems/climbing-stairs/description/
7
+ */
8
+function climbStairs(n: number): number {
9
+ if (n <= 2) {
10
+ return n;
11
+ }
12
+
13
+ let prev = 1;
14
+ let cur = 2;
15
16
+ for (let i = 3; i < n + 1; i++) {
17
+ const next = prev + cur;
18
+ prev = cur;
19
+ cur = next;
20
21
22
+ return cur;
23
+}
24
25
+// O(n) time
26
+// O(1) space
0 commit comments