-
Notifications
You must be signed in to change notification settings - Fork 21
1주차 과제 #13
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
Open
KJeeu
wants to merge
6
commits into
CodeSoom:main
Choose a base branch
from
KJeeu:jh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
1주차 과제 #13
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
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 |
|---|---|---|
| @@ -1,6 +1,35 @@ | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| const solution = (n) => { | ||
| const list = [1, 1]; | ||
| let count = 2; | ||
|
|
||
| while (count <= n) { | ||
| count += 1; | ||
| const sum = list.slice(-2).reduce((a, b) => a + b, 0); | ||
| list.push(sum); | ||
|
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. 연산할 때 마다 list가 계속 커지는데, 마지막 2개만 사용하는 것 같네요. 전전값과 바로 이전값만 유지하는 방법으로도 구현이 가능할 것 같습니다 |
||
| } | ||
|
|
||
| return n > 0 ? list[n - 1] : 0; | ||
| }; | ||
|
|
||
| // 2. 이번에는 재귀 함수로 문제를 해결해 주세요. | ||
| // 4번째 테스트 통과 못함 | ||
| // const solution = (n) => { | ||
| // if (n < 0) { | ||
| // return 0; | ||
| // } | ||
|
|
||
| // if (n === 0 || n === 1) { | ||
| // return n; | ||
| // } | ||
|
|
||
| // return solution(n - 1) + solution(n - 2); | ||
| // }; | ||
|
|
||
| // 3. 꼬리 재귀 함수로 바꿔보세요. | ||
| // 4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. | ||
| // 1번 풀이와 동일 | ||
|
|
||
| test('음수가 주어지면 0을 반환한다', () => { | ||
| expect(solution(-1)).toBe(0); | ||
| }); | ||
|
|
||
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
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
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
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
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.
여기서 작은 실수를 하나 하셨는데, 결과를 return하고 있지 않네요!