Skip to content

Commit fdd4d40

Browse files
committed
Solution: Climbing Stairs
1 parent 2695f42 commit fdd4d40

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

β€Žclimbing-stairs/flynn.mdβ€Ž

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Description
2+
3+
λ‹€μ΄λ‚˜λ―Ή ν”„λ‘œκ·Έλž˜λ°μ„ μ΄μš©ν•˜μ—¬ ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€.
4+
5+
μ•„λž˜μ™€ 같이 `memo` 배열을 μ •μ˜ν–ˆμ„ λ•Œ,
6+
7+
```
8+
memo[0] = 1
9+
memo[1] = 1
10+
memo[i] = distinct ways to climb to the i-th stair
11+
```
12+
13+
λ‹€μŒκ³Ό 같은 점화식이 μ„±λ¦½ν•©λ‹ˆλ‹€.
14+
15+
```
16+
memo[i] = memo[i - 2] + memo[i - 1] (i > 1)
17+
```
18+
19+
## Big-O
20+
21+
Time complexity: O(N)
22+
23+
Space complexity: O(N)
24+
25+
---
26+
27+
```cpp
28+
class Solution {
29+
public:
30+
int climbStairs(int n) {
31+
vector<int> memo(2, 1);
32+
33+
for (int i = 2; i <= n; i++) {
34+
memo.push_back(memo[i - 1] + memo[i - 2]);
35+
}
36+
37+
return memo[n];
38+
}
39+
};
40+
```

0 commit comments

Comments
Β (0)