-
-
Notifications
You must be signed in to change notification settings - Fork 304
[hongseoupyun] Week 02 solutions #2071
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
|
|
||
| # This problem is a getting the number of cases to climb stairs just taking 1 or 2 steps at a time. | ||
| # The pattern is similar to Fibonacci sequence. | ||
| # As n increases by 1, the number of ways to climb will be 1, 2, 3, 5, 8, 13, 21 | ||
| # The Formula is to get number of ways to climb to the nth step is: | ||
| # ways(n) = ways(n-1) + ways(n-2) with base of ways(1) = 1 and ways(2) = 2 | ||
| # which means the number of ways to climb is the sum of last two previous ways | ||
|
|
||
|
|
||
|
|
||
| # Time complexity is O(n) - loops n times | ||
| # Space complexity is O(1) — memorizes only prev1 & prev2 | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| # Handling base case | ||
| if n <= 1: | ||
| return 1 | ||
| if n == 2: | ||
| return 2 | ||
|
|
||
| prev1 = 1 | ||
| prev2 = 2 | ||
| for i in range(n,n+1): | ||
| current = prev2 + prev1 | ||
| prev1 = prev2 | ||
| prev2 = current | ||
|
|
||
| return prev2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // # This problem is a getting the number of cases to climb stairs just taking 1 or 2 steps at a time. | ||
| // # The pattern is similar to Fibonacci sequence. | ||
| // # As n increases by 1, the number of ways to climb will be 1, 2, 3, 5, 8, 13, 21 | ||
| // # The Formula is to get number of ways to climb to the nth step is: | ||
| // # ways(n) = ways(n-1) + ways(n-2) with base of ways(1) = 1 and ways(2) = 2 | ||
| // # which means the number of ways to climb is the sum of last two previous ways | ||
|
|
||
|
|
||
| //Time complexity is O(n) - loops n times | ||
| //Space complexity is O(1) — memorizes only prev1 & prev2 | ||
|
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. DP 테이블 없이 변수 2개만 유지함으로써 공간 복잡도를 최적화하셨네요! 👍🏻 |
||
| function climbStairs(n: number): number { | ||
|
|
||
|
|
||
| if (n <=1 ) return 1; | ||
| if (n === 2) return 2; | ||
|
|
||
| // initial settiongs | ||
| let prev1 = 1 | ||
| let prev2 = 2 | ||
|
|
||
| //Loop from when the number of stairs is 3 | ||
| for (let i = 3; i<=n; i++){ | ||
| const current = prev2 + prev1 | ||
| //eg) when n = 3, prev1 = 1, prev2 = 2, current = 3 => prev1 = 2, prev2 = 3, return prev2 | ||
| //eg) when n = 4, prev1 = 2, prev2 = 3, current = 5 => prev1 = 3, preev2 = 5, return prev2 | ||
| prev1 = prev2 | ||
| prev2 = current | ||
| } | ||
|
|
||
| return prev2 | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
range(n, n+1)이라면 루프가 딱 한 번만 실행될 것 같아요!range(3, n+1)을 쓰시려다 오타가 생긴 것 같습니다 ㅎㅎ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.
앗 제가 미쳐보지못한 실수네요.. 감사합니다!!
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.
@seungriyou