-
Notifications
You must be signed in to change notification settings - Fork 126
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
[moonhyeok] Week3 #801
Merged
Merged
[moonhyeok] Week3 #801
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
912fe12
feat: Upload two-sum(typescript)
mike2ox 121d8ce
feat: Upload reverse bits(typescript)
mike2ox 5a5bbab
feat: Upload maximum-subarray(typescript)
mike2ox 225d19e
feat: Upload product-of-array-except-self(typescript)
mike2ox 97a8b73
feat: Upload combination-sum(typescript)
mike2ox 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 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,27 @@ | ||
/** | ||
* source: https://leetcode.com/problems/combination-sum/ | ||
* 풀이방법: 재귀를 이용하여 모든 조합을 탐색 | ||
* | ||
* 시간복잡도: O(n^m) (n: candidates의 길이, m: target을 만들기 위한 최대 반복 횟수) | ||
* 공간복잡도: O(n^m) (n: candidates의 길이, m: target을 만들기 위한 최대 반복 횟수) | ||
* | ||
* Note | ||
* - 당장에 구현하려다보니 재귀를 이용한 방법으로 구현. => 추후 리팩토링 필요 | ||
*/ | ||
function combinationSum(candidates: number[], target: number): number[][] { | ||
if (target === 0) return [[]]; | ||
if (target < 0) return []; | ||
|
||
const result: number[][] = []; | ||
|
||
for (let i = 0; i < candidates.length; i++) { | ||
const num = candidates[i]; | ||
const subCombos = combinationSum(candidates.slice(i), target - num); | ||
|
||
for (const combo of subCombos) { | ||
result.push([num, ...combo]); | ||
} | ||
} | ||
|
||
return result; | ||
} |
Oops, something went wrong.
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.
코드가 가독성이 좋아서 읽기 편했어요! 감사합니다 :)
큰 차이는 없을 것 같지만 slice를 사용하면 배열 복사가 매번 발생하게 되어서 현재 idx를 파라미터로 넘기는 방법을 사용해도 괜찮을거 같아요!
로직 자체는 slice를 사용하는 것과 동일하게 현재 인덱스부터 끝까지 탐색한다는 면에서 같은 동작을 합니다.
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.
문제에서 제공하는 입력 형식 그대로 하려다보니 저렇게 되었는데 올려주신 방법이 훨씬 효율적이네요!
감사합니다 :)