Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions climbing-stairs/samthekorean.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# TC: O(n) — the loop runs n-2 times in the worst case
# SC: O(1) — only a constant number of variables are used (a, b, c)
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1 :
return 1
if n == 2 :
return 2

a = 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열이 아닌 변수를 사용한 공간 최적화. 좋습니다. 👍

b = 2

for i in range(n - 2) :
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수 i는 현재 코드에서 사용되고 있지 않습니다.
for _ in range(n-2): 로 변경하시면, 의도와 동일하게 동작합니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그 부분을 놓쳤군요! 피드백 감사합니다!

c = a + b
a = b
b = c

return b


# 이전 풀이
# Time complexity : O(n)
# Space complexity : O(1)
class Solution:
Expand All @@ -16,3 +37,4 @@ def climbStairs(self, n: int) -> int:
a, b = b, result
Comment on lines 24 to 37
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반복문 내부에서 a, b = b, result 형태로 상태를 업데이트하려고 하신 것 맞을까요? 😄
코드 일부만 보여서 전체 로직을 정확히 파악하기가 조금 헷갈립니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 2년전 1기때 풀었던 문제라서 문맥이 부족했던 것 같습니다! ㅎㅎ 그 당시에는 말씀하신걸 의도한게 맞습니다! 꼼꼼한 리뷰 감사합니다!


return result

Loading