-
-
Notifications
You must be signed in to change notification settings - Fork 339
[SAM] Week 2 solutions #2418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SAM] Week 2 solutions #2418
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| b = 2 | ||
|
|
||
| for i in range(n - 2) : | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변수 i는 현재 코드에서 사용되고 있지 않습니다.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
@@ -16,3 +37,4 @@ def climbStairs(self, n: int) -> int: | |
| a, b = b, result | ||
|
Comment on lines
24
to
37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반복문 내부에서
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 2년전 1기때 풀었던 문제라서 문맥이 부족했던 것 같습니다! ㅎㅎ 그 당시에는 말씀하신걸 의도한게 맞습니다! 꼼꼼한 리뷰 감사합니다! |
||
|
|
||
| return result | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배열이 아닌 변수를 사용한 공간 최적화. 좋습니다. 👍