Skip to content

Commit dd58e5e

Browse files
committed
Add solution for Climbing Stairs
1 parent b1310d2 commit dd58e5e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

β€Žclimbing-stairs/gcount85.pyβ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
# Intuition
3+
λ§ˆμ§€λ§‰ 계단에 λ„μ°©ν•˜λŠ” 경우의 μˆ˜λŠ”, 2μŠ€ν… μ „μ˜ 경우의 μˆ˜μ™€ 1μŠ€ν… μ „μ˜ 경우의 수λ₯Ό ν•©ν•œ κ°’μž…λ‹ˆλ‹€.
4+
5+
# Approach
6+
dp[n] = dp[n-1] + dp[n-2]
7+
8+
# Complexity
9+
- Time complexity: O(n)
10+
11+
- Space complexity: O(1)
12+
"""
13+
14+
15+
class Solution:
16+
def climbStairs(self, n: int) -> int:
17+
b = 1 # n이 1μΌλ•Œ
18+
if n == 1:
19+
return b
20+
a = 2 # n이 2μΌλ•Œ
21+
if n == 2:
22+
return a
23+
24+
for n in range(3, n + 1): # O(n)
25+
a, b = a + b, a
26+
return a

0 commit comments

Comments
Β (0)