Skip to content

Commit f86c6e9

Browse files
committed
sadie100: climbing-stairs solution
1 parent a057cfd commit f86c6e9

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

climbing-stairs/sadie100.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
3+
n+1개의 배열을 만들고 순회를 돌리며 i-1, i-2를 합한다
4+
5+
시간복잡도 : O(N) => n 배열 순회
6+
공간복잡도 : O(N) => n+1 배열
7+
*/
8+
9+
function climbStairs(n: number): number {
10+
const stair = new Array(n + 1).fill(0)
11+
12+
stair[0] = 1
13+
stair[1] = 1
14+
15+
if (n < 1) return stair[n]
16+
17+
for (let i = 2; i < n + 1; i++) {
18+
stair[i] = stair[i - 1] + stair[i - 2]
19+
}
20+
21+
return stair[n]
22+
}

0 commit comments

Comments
 (0)