Skip to content

Commit e061a11

Browse files
committed
climbing-stairs
1 parent cf9d00c commit e061a11

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

climbing-stairs/taewanseoul.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)