Skip to content
Merged
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions climbing-stairs/hi-rachel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ def climbStairs(self, n: int) -> int:
for i in range(2, n):
steps[i] = steps[i - 2] + steps[i - 1]
return steps[n - 1]

"""
변수 2개로 최적화
공간 복잡도 O(1) 개선 풀이
"""
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1
elif n == 2:
return 2

prev1 = 1
prev2 = 2

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

Choose a reason for hiding this comment

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

range 루프에서 index를 사용하지 않기에 _ 사용 적절해보입니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@printjin-gmailcom 아 최적화하면서 index를 사용하지 않는 부분을 놓쳤네요. 감사합니다.

prev1, prev2 = prev2, prev1 + prev2
return prev2