Skip to content

Commit 59406aa

Browse files
authored
Merge pull request DaleStudy#2418 from SamTheKorean/main
[SAM] Week 2 solutions
2 parents 2cfd02a + 7d316e1 commit 59406aa

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

climbing-stairs/samthekorean.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# TC: O(n) — the loop runs n-2 times in the worst case
2+
# SC: O(1) — only a constant number of variables are used (a, b, c)
3+
class Solution:
4+
def climbStairs(self, n: int) -> int:
5+
if n == 1 :
6+
return 1
7+
if n == 2 :
8+
return 2
9+
10+
a = 1
11+
b = 2
12+
13+
for i in range(n - 2) :
14+
c = a + b
15+
a = b
16+
b = c
17+
18+
return b
19+
20+
21+
# 이전 풀이
122
# Time complexity : O(n)
223
# Space complexity : O(1)
324
class Solution:
@@ -16,3 +37,4 @@ def climbStairs(self, n: int) -> int:
1637
a, b = b, result
1738

1839
return result
40+

0 commit comments

Comments
 (0)