-
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
[mallayon] Week 3 #792
Merged
Merged
[mallayon] Week 3 #792
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 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,37 @@ | ||
/** | ||
* 접근 방법 : | ||
* - 중복 포함하여 모든 조합 구해야 하니까 재귀함수로 풀기 | ||
* - 재귀 호출로 탐색해야하는 타겟 줄여가면서 조합 만들기 | ||
* - 동일 조합추가되지 않도록, startIndex 추가하여 다음 인덱스부터 순회하도록 제한 | ||
* | ||
* | ||
* 시간복잡도 : O(n^target) | ||
* - candidates 배열 길이 n만큼 재귀가 호출되고, 각 호출은 target 길이 만큼 중첩되니까 O(n^target) | ||
* | ||
* 공간복잡도 : O(target) | ||
* - 최악의 경우 target만큼 재귀 호출되니까 O(target) | ||
* | ||
*/ | ||
|
||
function combinationSum(candidates: number[], target: number): number[][] { | ||
const result: number[][] = []; | ||
|
||
const dfs = (target: number, combination: number[], startIndex: number) => { | ||
if (target === 0) { | ||
result.push([...combination]); | ||
return; | ||
} | ||
|
||
if (target < 0) return; | ||
|
||
for (let i = startIndex; i < candidates.length; i++) { | ||
combination.push(candidates[i]); | ||
dfs(target - candidates[i], combination, i); | ||
combination.pop(); | ||
} | ||
}; | ||
|
||
dfs(target, [], 0); | ||
|
||
return result; | ||
} |
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,31 @@ | ||
/** | ||
* | ||
* 접근 방법 : | ||
* - O(n)으로 풀어야 하니까 중첩이 아닌 배열 개별로 2번 순회 방법으로 접근 | ||
* - 왼쪽 곱(prefixProduct)과 오른쪽 곱((suffixProduct)을 따로 계산해서 결과값에 저장 | ||
* | ||
* 시간복잡도 : O(n) | ||
* - 배열 길이만큼 순회하니까 O(n) | ||
* | ||
* 공간복잡도 : O(n) | ||
* - 배열 길이만큼 결과값 저장하니까 O(n) | ||
* | ||
*/ | ||
|
||
function productExceptSelf(nums: number[]): number[] { | ||
let result: number[] = Array(nums.length).fill(1); | ||
let prefixProduct = 1; | ||
let suffixProduct = 1; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
result[i] = prefixProduct; | ||
prefixProduct *= nums[i]; | ||
} | ||
|
||
for (let i = nums.length - 1; i >= 0; i--) { | ||
result[i] *= suffixProduct; | ||
suffixProduct *= nums[i]; | ||
} | ||
|
||
return result; | ||
} |
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,31 @@ | ||
/** | ||
* | ||
* 접근 방법 : | ||
* - 이미 방문한 숫자와 인덱스를 맵에 저장 | ||
* - nums 배열 순회하면서, 찾는 숫자 가 없으면 맵에 값, 인덱스 추가하기 | ||
* - 맵에 존재하면 현재 인덱스와, 해당 숫자의 인덱스 담아서 즉시 리턴하기 | ||
* | ||
* 시간복잡도 : O(n) | ||
* - nums 배열 길이만큼 1회 순회하니까 O(n) | ||
* - 맵 조회 및 삽입은 O(1) | ||
* | ||
* 공간복잡도 : O(n) | ||
* - 맵에 배열의 값 저장하니까 O(n) | ||
* | ||
* 엣지 케이스 : | ||
* - 동일한 숫자가 있는 경우 : [3, 3], 6 => [0,1] | ||
*/ | ||
|
||
function twoSum(nums: number[], target: number): number[] { | ||
const map = new Map<number, number>(); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const neededValue = target - nums[i]; | ||
|
||
if (map.has(neededValue)) { | ||
return [map.get(neededValue)!, i]; | ||
} | ||
|
||
map.set(nums[i], i); | ||
} | ||
} |
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.
nums.length
가 여러번 쓰이는 것 같아요. 변수화를 통해 가독성을 조금 더 높여봐도 좋을 것 같아요!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.
nums.length
를 변수에 저장하는 것은 호불호가 갈릴 수도 있다는 생각이 들었습니다. 저는 오히려 기억해야 할 변수가 하나 줄어서 코드를 읽기 더 편했던 것 같아요 :)